10

I am able to use the

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

But I am facing a problem in changing the controller's Name. Is there some annotation available to make controller name hyphen (-) separated in MVC 4?

Somewhat like this:

[ControllerName("My-Controller-Name")]
public class MyControllerName : Controller
{

}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Shan k
  • 199
  • 1
  • 13

4 Answers4

9

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())
        );
HasanG
  • 12,734
  • 29
  • 100
  • 154
aleha_84
  • 8,309
  • 2
  • 38
  • 46
  • In case you need a route name, see [below](http://stackoverflow.com/a/32716734/456456) for a alternative using MapRoute. – R. Schreurs Sep 22 '15 at 12:22
5

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) { ... }
}
Jake Braun
  • 1,172
  • 1
  • 13
  • 34
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • 2
    It might be useful to provide a couple of examples rather than just a link. – Ben Robinson Oct 03 '14 at 09:03
  • Attribute routing *only* works in MVC 5. You can't use it in MVC 4 – Panagiotis Kanavos Oct 03 '14 at 09:03
  • 1
    There is a nuget package for that. yes you can. – DarthVader Oct 03 '14 at 09:03
  • Then the answer should make it explicit that it *doesn't* refer to ASP.NET MVC's attribute routing, but a 2 year old nuget package. The two implementations are *not* compatible. The OP may prefer to upgrade to MVC 5 instead of facing the migration issues later on – Panagiotis Kanavos Oct 03 '14 at 09:07
  • 1
    why dont you provide an answer with those details? I will +1. – DarthVader Oct 03 '14 at 09:08
  • Guys, We can do this in RouteConfig.cs file, by overriding some method, but I am not able to do that right away. Although I have seen the solution in one of my previous project, but no luck now. Can you please help. – Shan k Oct 03 '14 at 09:16
1

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();
R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
0

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();
    }
}
Lsakurifaisu
  • 133
  • 1
  • 12