how to get action with specific url asp mvc - asp.net

i want get specific link to receive an action
this my controller :
namespace tabi.Controllers
{
public class CategoryController : Controller
{
public ActionResult List(string name=null)
{
ViewBag.name = name;
return View();
}
}
}
how to get action with this link :
/category/game
game is name parameter value
and don't change default route

If you haven't defined a custom route, you have to use the following url:
/Category/List?name=game
If you specify a custom route to allow List as the default action, and /{name} to the route (rather than ID), it would utilize the route you specified, such as:
routes.MapRoute(
name: "game",
url: "{controller}/{name}",
defaults: new { controller = "Home", action = "List" });
This route should support that URL.

Related

How to set another page as default page in ASPNET BOILERPLATE MVC5?

I'm trying change default page when I start my app, but I can't do it. When I start the first page is "Account/Login", but I need it changes to other pages.
In project web I'm doing this: * HomeController: Add HomePage actionResult * View/Home: Add View to Home with name HomePage
In app_start/routeconfig.cs
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "HomePage", id = UrlParameter.Optional } );
Also, i changed the properties of web project to specify page: Home/Homepage, but it's not working
Results in both options arent work
Template: Boilerplate with ASP.NET MVC 5 + Zero Module
i'm new with abp.
In HomeController.cs, comment out (or remove) the [AbpMvcAuthorize] attribute:
// [AbpMvcAuthorize]
public class HomeController : AbpProjectNameControllerBase
You can apply [AllowAnonymous] for specific actions to suppress authentication/authorization:
[AbpMvcAuthorize]
public class HomeController : AbpProjectNameControllerBase
{
public ActionResult Index()
{
return View();
}
[AllowAnonymous]
public ActionResult HomePage()
{
return View();
}
}
See the documentation on MVC Controllers.
Maybe it's better to use a different controller for anonymous actions. Create a new controller called WelcomeController. Do not add a AbpMvcAuthroize attribute. Then set your default route as Welcome/Index.

ActionLink ignoring attribute routing settings

I have a TextObject controller, which is meant to be accessed by "~/umt/text/{action}/{id?}", as defined in the controller using attribute routing, but the action link:
#Html.ActionLink("Index", "Index", "TextObject")
ignores Attribute Routing and uses the Conventional routing definitions, producing ~/TextObject/ instead of the desired ~/umt/text/
the TextObjectController:
[Authorize]
[RouteArea("umt")]
[RoutePrefix("text")]
[Route("{action=index}/{id?}")]
public class TextObjectController : Controller
{
.....
public async Task<ActionResult> Index()
{
return View(await db.TextObjects.ToListAsync());
}
.....
}
My route config:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Enable Attribute Routing
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Is there any additional configuration required on the controller to make the action link work or does it not work with attribute routing?
I'd like to keep it simple, and it routes correctly going directly through the url, but the ActionLink helper seems to not like something about it.
I can't see that you specify your defauld area it RouteConfig so your action link should look like:
#Html.ActionLink("TextObject", "Index", "Index", new { area = "umt" }, null)

the request is invalid?

Bit of a asp.net mvc noob , I am trying to pass in a string as an argument for my Web API controller:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
public string Get(string arg)
{
return "othervalue";
}
}
I tried to add another route:
routes.MapRoute(
name: "Default2",
url: "{controller}/{action}/{arg}",
defaults: new { controller = "Home", action = "Index", arg = UrlParameter.Optional }
);
So I want to keep both Get methods and use the Get with the arg parameter so I can pass in a string. So when I try to hit this url 'api/values/jjhjh' in my browser I get this error:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Get(Int32)' in 'stackOverflowWebApi.Controllers.ValuesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
The additional route you added was an MVC route, not a WebAPI route. WebAPI routes are not located in RouteConfig.cs by default, they are in WebApiConfig.cs. They look more like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
The error you posted comes from not passing in any data at all. Give this a try instead:
public string Get(string id = null)
{
return "othervalue";
}
Note that the parameter name is id, not arg, to make it match the optional route parameter. Also, defaulting it to null tells the binder that its okay to invoke this method when no data is passed.

Common controller, multiple areas - need routing?

I have a controller defined in a library. I'd like this controller to be accessible via any of my 3 areas. At the moment, the controller (let's say "contact") is not being found when accessed via for example the "admin" area (i.e. url of /admin/contact). It does however work when accessed via "/contact".
Is there any route configuration required to Areas in order to allow the access of a common controller though these areas?
Thanks.
You could put this controller in a namespace:
namespace MvcApplication1.Controllers.MyAreas
{
public class ContactsController : Controller
{
...
}
}
and then in your area registration specify this namespace:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "SomeLibrary.Controllers.MyAreas" }
);
}
Now when you navigate to /admin/contacts/index the Index action of the ContactsController will be executed.

ASP.NET MVC - Routes

I'm working on an MVC application and I have and admin area... So what I need is:
When user makes request to admin (for example "/Admin/Post/Add") I need to map this to controller AdminPost and action Add... is it possible?
If your controller is named AdminPostController and you want it to map to '/Admin/Post/Add' then you can use:
routes.MapRoute("Admin", // Route name
"Admin/Post/{action}/{id}", // URL with parameters
new { controller = "AdminPost", action = "Add", id = "" } // Parameter defaults
);
Note the use of the parameter defaults.
If your controller is named AdminController and you just wanted to separate the request method then use the default:
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
Which will map '/Admin/Add/' to the controller:
public class AdminController : Controller {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(int id) {
//...
}
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Add(int id) {
//...
}
}
Note the use of [AcceptVerbs] to identify which method to invoke for POST requests and GET requests.
See Scott Gu's blog for more details

Resources