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

后端 未结 4 1212
小蘑菇
小蘑菇 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条回答
  • 2021-01-05 01:47

    You can use Attribute Routing.

    It comes in MVC 5 as well.

    You can find some examples below.

    http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

    [RoutePrefix("Book-Reviews")]
    public class ReviewsController : Controller
    {
        // eg.: /reviews
        [Route]
        public ActionResult Index() { ... }
        // eg.: /reviews/5
        [Route("{reviewId}")]
        public ActionResult Show(int reviewId) { ... }
        // eg.: /reviews/5/edit
        [Route("{reviewId}/edit")]
        public ActionResult Edit(int reviewId) { ... }
    }
    
    0 讨论(0)
  • 2021-01-05 01:49

    Here is a good answer:

    Add custom route handler (in replace part choose how you want to handle hyphens):

    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);
        }
    }
    

    and use it in your RouteConfig

    routes.Add(
            new Route("{controller}/{action}/{id}",
                new RouteValueDictionary(
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
                    new HyphenatedRouteHandler())
            );
    
    0 讨论(0)
  • 2021-01-05 01:52

    I like the answer of @aleha, but wanted to do this for MapRoute, because I need a route name.

    In such cases, you can do this:

        Route route = routes.MapRoute(
            "RouteName",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    
        route.RouteHandler = new HyphenatedRouteHandler();
    
    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题