Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.Net MVC: How do I enable dashes in my urls?

Tags:

asp.net-mvc

I'd like to have dashes separate words in my URLs. So instead of:

/MyController/MyAction

I'd like:

/My-Controller/My-Action

Is this possible?

like image 974
Jim Geurts Avatar asked Aug 27 '08 14:08

Jim Geurts


3 Answers

You can use the ActionName attribute like so:

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

Note that you will then need to call your View file "My-Action.cshtml" (or appropriate extension). You will also need to reference "my-action" in any Html.ActionLink methods.

There isn't such a simple solution for controllers.

Edit: Update for MVC5

Enable the routes globally:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapMvcAttributeRoutes();
    // routes.MapRoute...
}

Now with MVC5, Attribute Routing has been absorbed into the project. You can now use:

[Route("My-Action")]

On Action Methods.

For controllers, you can apply a RoutePrefix attribute which will be applied to all action methods in that controller:

[RoutePrefix("my-controller")]

One of the benefits of using RoutePrefix is URL parameters will also be passed down to any action methods.

[RoutePrefix("clients/{clientId:int}")]
public class ClientsController : Controller .....

Snip..

[Route("edit-client")]
public ActionResult Edit(int clientId) // will match /clients/123/edit-client
like image 86
ChadT Avatar answered Nov 16 '22 20:11

ChadT


You could create a custom route handler as shown in this blog:

http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/

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 the new route:

routes.Add(
            new Route("{controller}/{action}/{id}", 
                new RouteValueDictionary(
                    new { controller = "Default", action = "Index", id = "" }),
                    new HyphenatedRouteHandler())
        );

A very similar question was asked here: ASP.net MVC support for URL's with hyphens

like image 16
Andrew Avatar answered Nov 16 '22 21:11

Andrew


I've developed an open source NuGet library for this problem which implicitly converts EveryMvc/Url to every-mvc/url.

Uppercase urls are problematic because cookie paths are case-sensitive, most of the internet is actually case-sensitive while Microsoft technologies treats urls as case-insensitive. (More on my blog post)

NuGet Package: https://www.nuget.org/packages/LowercaseDashedRoute/

To install it, simply open the NuGet window in the Visual Studio by right clicking the Project and selecting NuGet Package Manager, and on the "Online" tab type "Lowercase Dashed Route", and it should pop up.

Alternatively, you can run this code in the Package Manager Console:

Install-Package LowercaseDashedRoute

After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:

routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",
  new RouteValueDictionary(
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),
    new DashedRouteHandler()
  )
);

That's it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.

Open Source Project Url: https://github.com/AtaS/lowercase-dashed-route

like image 11
Ata S. Avatar answered Nov 16 '22 22:11

Ata S.