How to make the controller's name hyphen “-” separated?

后端 未结 4 1214
小蘑菇
小蘑菇 2021-01-05 01:08

I am able to use the

[ActionName(\"My-Action-Name\")]
public ActionResult MyActionName()
{
    return View();
}

But I am facing a problem

4条回答
  •  -上瘾入骨i
    2021-01-05 02:07

    Thanks to @R. Schreurs solutions. This is what my RouteConfig class looks like now and my controller name now works with a hyphen.

    public class RouteConfig
    {
        public class HyphenatedRouteHandler : MvcRouteHandler
        {
            protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "");
                requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "");
                return base.GetHttpHandler(requestContext);
            }
        }
    
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            ).RouteHandler = new HyphenatedRouteHandler();
        }
    }
    

提交回复
热议问题