How to achieve asp.net mvc action name with hyphen e.g., www.domain.com/about-us
where about-us
is the acton name in home controller. With this approach, i can achieve to have the action name like contact-us
, how-to
, etc.
Here's the complete working solution to the problem for a .Net MVC 5 project:
Open your project's App_Start/RouteConfig.cs file.
On your RegisterRoute method, modify the codes like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapMvcAttributeRoutes(); // <--- add this line to enable custom attribute routes
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Now, open your controller file then add your custom route's definition at the top your action as shown below:
[Route("about-us")]
public ActionResult AboutUs()
{
return View();
}
You should now be able to use your custom route like this:
http://yourdomain.com/about-us
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With