Reputation: 21430
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.
Upvotes: 2
Views: 921
Reputation: 49095
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.
Upvotes: 1
Reputation: 55042
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.
Upvotes: 4