I have an action in my HomeController
like this:
public ActionResult HowItWorks()
{
return View();
}
I can access this by going to /Home/HowItWorks
as you might expect.
How can I make it so that it will go to the same place if I go to /HowItWorks
(omitting the "Home" prefix)?
I know I can change this in RouteConfig.cs
but I was wondering if there was an attribute or something like that.
routes.MapRoute(
name: "HowItWorks",
url: "HowItWorks",
defaults: new { controller = "Home", action = "HowItWorks", id = UrlParameter.Optional }
);
You can do it as above. and yes, you need to put it in RouteConfig.cs
If you want all your methods work like that you can use following:
routes.MapRoute(
name: "HomePages",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In this case though, you can use a single controller only, if and only if you dont define a custom route as follows:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Note that precedence of routes matters. ie: whichever is matched first will be embraced.
I know I can change this in RouteConfig.cs but I was wondering if there was an attribute or something like that.
Take a look at AttributeRouting , it's pretty cool.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.