[英]How to achieve asp.net mvc action name with hyphen?
How to achieve asp.net mvc action name with hyphen eg, www.domain.com/about-us
where about-us
is the acton name in home controller. 如何用连字符实现asp.net mvc动作名称,例如www.domain.com/about-us
,其中about-us
是家庭控制器中的acton名称。 With this approach, i can achieve to have the action name like contact-us
, how-to
, etc. 通过这种方法,我可以实现有动作名称,如contact-us
, how-to
,等等。
You can give your action name inside of actionName
tag. 您可以在actionName
标记内提供操作名称。
[ActionName("about-us")]
public ActionResult AboutUs() {
return View();
}
Since nobody is willing to give the complete working solution to the problem, here's how it's done in a .Net MVC 5 project: 由于没有人愿意为问题提供完整的工作解决方案,因此在.Net MVC 5项目中如何完成:
On your RegisterRoute method, modify the codes like this: 在RegisterRoute方法上,修改如下代码:
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
In AreaRegistration.cs
file, you can add route table entry as below..... 在AreaRegistration.cs
文件中,您可以添加以下路由表条目.....
context.MapRoute(
"about_us_default",
"about-us",
new { action = "AboutUs" }
);
In MVC5 you are able to set up attribute routes too: 在MVC5中,您也可以设置属性路由:
[Route("about-us")]
public ActionResult AboutUs()
{
return View();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.