4

I am struggling how to properly allow dashes / hypens in a URL in my MVC 4 / ASP.NET 4.5 app...for some reason MVC converts the dashes to underscores which is not what i want.

I've done a good bit of searching around before i posted the question, still can't find any easy solution. Everything i found seems way out of whack for such a simple configuration.

Basically, i would like to be able to access the following URL:

www.mysite.com/dashes-in-url

So for example, i created a controller named:

dashes-in-urlController.cs

The controller was named fine and allowed the dashes in the controller name.

However, when i created a view for the Index for the above controller, it created the view as:

Folder: /Views/dashes_in_url

With an Index.cshtml file in that folder - but it replaced my dashes with underscores.

So i have to access the url as:

www.mysite.com/dashes_in_url

with underscores instead of with the dashes in the URL.

Does anyone know of any elegant, simple solution to achieve what i'm trying to do?

Any help in this matter would be greatly appreciated, thanks!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
KabanaSoft
  • 1,591
  • 2
  • 13
  • 13
  • Silly question...Did you try to rename your view like "dashes-in-url.cshtml"? – Pablo Claus Sep 15 '12 at 02:27
  • 2
    You need to implement a HyphenatedRouteHandler - thankfully lots of people have done this already so checkout the accepted answer here http://stackoverflow.com/questions/2070890/asp-net-mvc-support-for-urls-with-hyphens or search google for HyphenatedRouteHandler for more examples – Luke Baughan Sep 20 '12 at 18:03

1 Answers1

7

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

Dashed urls are much more SEO friendly and easier to read. (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

Ata S.
  • 890
  • 7
  • 12
  • When I use javascript to reference an address such as window.location.href = "/MyController/Index/Id/ArbitraryText" the controller is not dashed. How can I fix this? – Shane LeBlanc Sep 19 '13 at 23:17
  • Can you send the sample codes to let me see the issue clearly? Please open an issue at here: https://github.com/AtaS/lowercase-dashed-route/issues – Ata S. Sep 20 '13 at 11:26
  • Another question regarding this package. When I type in a url localhost:5038/MyHomeAction it works great. But what about if I want to use a different controller like say my controller name is CodesController and I want to go to localhost:5038/Codes I get a 404 because it seems to look for the action "Codes" in the HomeController rather than realize there isn't one and hit the CodesController and then the Index Action of such controller. – Shane LeBlanc Sep 25 '13 at 03:43
  • Well for me, it is not like that. For example I have AdminController and I can enter it by typing /admin. Even Html.ActionLink() generates the /admin link. It works the same with or without Lowercase Dashed Route package for me. You may give another example if you think there's still a problem though. – Ata S. Sep 26 '13 at 10:03