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

<pre><code>/MyController/MyAction
</code></pre>

<p>I'd like:</p>

<pre><code>/My-Controller/My-Action
</code></pre>

<p>Is this possible?</p>

[{'answer_id': 30559, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 1, 'selected': False, 'text': "<p>You could write a custom route that derives from the Route class GetRouteData to strip dashes, but when you call the APIs to generate a URL, you'll have to remember to include the dashes for action name and controller name.</p>\n\n<p>That shouldn't be too hard.</p>\n"}, {'answer_id': 540583, 'author': 'ChadT', 'author_id': 23300, 'author_profile': 'https://Stackoverflow.com/users/23300', 'pm_score': 8, 'selected': True, 'text': '<p>You can use the ActionName attribute like so:</p>\n\n<pre><code>[ActionName("My-Action")]\npublic ActionResult MyAction() {\n    return View();\n}\n</code></pre>\n\n<p>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.</p>\n\n<p>There isn\'t such a simple solution for controllers.</p>\n\n<h2>Edit: Update for MVC5</h2>\n\n<p>Enable the routes globally: </p>\n\n<pre><code>public static void RegisterRoutes(RouteCollection routes)\n{\n    routes.MapMvcAttributeRoutes();\n    // routes.MapRoute...\n}\n</code></pre>\n\n<p>Now with MVC5, Attribute Routing has been absorbed into the project. You can now use:</p>\n\n<pre><code>[Route("My-Action")]\n</code></pre>\n\n<p>On Action Methods. </p>\n\n<p>For controllers, you can apply a <code>RoutePrefix</code> attribute which will be applied to all action methods in that controller:</p>\n\n<pre><code>[RoutePrefix("my-controller")]\n</code></pre>\n\n<p>One of the benefits of using <code>RoutePrefix</code> is URL parameters will also be passed down to any action methods.</p>\n\n<pre><code>[RoutePrefix("clients/{clientId:int}")]\npublic class ClientsController : Controller .....\n</code></pre>\n\n<p>Snip..</p>\n\n<pre><code>[Route("edit-client")]\npublic ActionResult Edit(int clientId) // will match /clients/123/edit-client\n</code></pre>\n'}, {'answer_id': 2454016, 'author': 'Andrew', 'author_id': 73228, 'author_profile': 'https://Stackoverflow.com/users/73228', 'pm_score': 4, 'selected': False, 'text': '<p>You could create a custom route handler as shown in this blog:</p>\n\n<p><a href="http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/" rel="nofollow noreferrer">http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/</a></p>\n\n<pre><code>public class HyphenatedRouteHandler : MvcRouteHandler{\n        protected override IHttpHandler  GetHttpHandler(RequestContext requestContext)\n        {\n            requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString().Replace("-", "_");\n            requestContext.RouteData.Values["action"] = requestContext.RouteData.Values["action"].ToString().Replace("-", "_");\n            return base.GetHttpHandler(requestContext);\n        }\n    }\n</code></pre>\n\n<p>...and the new route:</p>\n\n<pre><code>routes.Add(\n            new Route("{controller}/{action}/{id}", \n                new RouteValueDictionary(\n                    new { controller = "Default", action = "Index", id = "" }),\n                    new HyphenatedRouteHandler())\n        );\n</code></pre>\n\n<p>A very similar question was asked here: <a href="https://stackoverflow.com/questions/2070890/asp-net-mvc-support-for-urls-with-hyphens">ASP.net MVC support for URL&#39;s with hyphens</a></p>\n'}, {'answer_id': 10488825, 'author': 'Nexxas', 'author_id': 297656, 'author_profile': 'https://Stackoverflow.com/users/297656', 'pm_score': 1, 'selected': False, 'text': '<p>You can define a specific route such as:</p>\n\n<pre><code>routes.MapRoute(\n    "TandC", // Route controllerName\n    "CommonPath/{controller}/Terms-and-Conditions", // URL with parameters\n    new {\n        controller = "Home",\n        action = "Terms_and_Conditions"\n    } // Parameter defaults\n);\n</code></pre>\n\n<p>But this route has to be registered <strong>BEFORE</strong> your default route.</p>\n'}, {'answer_id': 13771284, 'author': 'Cory Mawhorter', 'author_id': 670023, 'author_profile': 'https://Stackoverflow.com/users/670023', 'pm_score': 0, 'selected': False, 'text': '<p>If you have access to the IIS URL Rewrite module ( <a href="http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx" rel="nofollow">http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx</a> ), you can simply rewrite the URLs.</p>\n\n<p>Requests to /my-controller/my-action can be rewritten to /mycontroller/myaction and then there is no need to write custom handlers or anything else.  Visitors get pretty urls and you get ones MVC can understand.</p>\n\n<p>Here\'s an example for one controller and action, but you could modify this to be a more generic solution:</p>\n\n<pre><code>&lt;rewrite&gt;\n  &lt;rules&gt;\n    &lt;rule name="Dashes, damnit"&gt;\n      &lt;match url="^my-controller(.*)" /&gt;\n      &lt;action type="Rewrite" url="MyController/Index{R:1}" /&gt;\n    &lt;/rule&gt;\n  &lt;/rules&gt;\n&lt;/rewrite&gt;\n</code></pre>\n\n<p>The possible downside to this is you\'ll have to switch your project to use IIS Express or IIS for rewrites to work during development.</p>\n'}, {'answer_id': 17454384, 'author': 'gregtheross', 'author_id': 2528526, 'author_profile': 'https://Stackoverflow.com/users/2528526', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m still pretty new to MVC, so take it with a grain of salt.  It\'s not an elegant, catch-all solution but did the trick for me in MVC4:</p>\n\n<pre class="lang-cs prettyprint-override"><code>routes.MapRoute(\n    name: "ControllerName",\n    url: "Controller-Name/{action}/{id}",\n    defaults: new { controller = "ControllerName", action = "Index", id = UrlParameter.Optional }\n);\n</code></pre>\n'}, {'answer_id': 18032530, 'author': 'Ata S.', 'author_id': 1216609, 'author_profile': 'https://Stackoverflow.com/users/1216609', 'pm_score': 4, 'selected': False, 'text': '<p>I\'ve developed an open source <strong>NuGet library</strong> for this problem which implicitly converts EveryMvc/Url to every-mvc/url. </p>\n\n<p>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. (<a href="http://www.ata.io/lowercase-dashed-route/">More on my blog post</a>)</p>\n\n<p>NuGet Package: <a href="https://www.nuget.org/packages/LowercaseDashedRoute/">https://www.nuget.org/packages/LowercaseDashedRoute/</a></p>\n\n<p>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.</p>\n\n<p>Alternatively, you can run this code <strong>in the Package Manager Console:</strong></p>\n\n<p><code>Install-Package LowercaseDashedRoute</code></p>\n\n<p>After that you should open App_Start/RouteConfig.cs and comment out existing route.MapRoute(...) call and add this instead:</p>\n\n<pre><code>routes.Add(new LowercaseDashedRoute("{controller}/{action}/{id}",\n  new RouteValueDictionary(\n    new { controller = "Home", action = "Index", id = UrlParameter.Optional }),\n    new DashedRouteHandler()\n  )\n);\n</code></pre>\n\n<p>That\'s it. All the urls are lowercase, dashed, and converted implicitly without you doing anything more.</p>\n\n<p>Open Source Project Url: <a href="https://github.com/AtaS/lowercase-dashed-route">https://github.com/AtaS/lowercase-dashed-route</a></p>\n'}, {'answer_id': 18043489, 'author': 'Jim Geurts', 'author_id': 3085, 'author_profile': 'https://Stackoverflow.com/users/3085', 'pm_score': 2, 'selected': False, 'text': '<p>Asp.Net MVC 5 will support attribute routing, allowing more explicit control over route names. Sample usage will look like:</p>\n\n<pre><code>[RoutePrefix("dogs-and-cats")]\npublic class DogsAndCatsController : Controller\n{\n    [HttpGet("living-together")]\n    public ViewResult LivingTogether() { ... }\n\n    [HttpPost("mass-hysteria")]\n    public ViewResult MassHysteria() { }\n}\n</code></pre>\n\n<p>To get this behavior for projects using Asp.Net MVC prior to v5, similar functionality can be found with the <a href="http://attributerouting.net/" rel="nofollow">AttributeRouting project</a> (also <a href="http://www.nuget.org/packages/AttributeRouting/" rel="nofollow">available as a nuget</a>). In fact, Microsoft reached out to the author of AttributeRouting to help them with their implementation for MVC 5.</p>\n'}, {'answer_id': 21173406, 'author': 'Daniel Eagle', 'author_id': 1727203, 'author_profile': 'https://Stackoverflow.com/users/1727203', 'pm_score': 3, 'selected': False, 'text': '<p>Here\'s what I did using areas in ASP.NET MVC 5 and it worked liked a charm. I didn\'t have to rename my views, either.</p>\n\n<p>In RouteConfig.cs, do this:</p>\n\n<pre><code> public static void RegisterRoutes(RouteCollection routes)\n    {\n        // add these to enable attribute routing and lowercase urls, if desired\n        routes.MapMvcAttributeRoutes();\n        routes.LowercaseUrls = true;\n\n        // routes.MapRoute...\n    }\n</code></pre>\n\n<p>In your controller, add this before your class definition:</p>\n\n<pre><code>[RouteArea("SampleArea", AreaPrefix = "sample-area")]\n[Route("{action}")]\npublic class SampleAreaController: Controller\n{\n    // ...\n\n    [Route("my-action")]\n    public ViewResult MyAction()\n    {\n        // do something useful\n    }\n}\n</code></pre>\n\n<p>The URL that shows up in the browser if testing on local machine is: <strong>localhost/sample-area/my-action</strong>. You don\'t need to rename your view files or anything. I was quite happy with the end result.</p>\n\n<p>After routing attributes are enabled you can delete any area registration files you have such as SampleAreaRegistration.cs.</p>\n\n<p>This <a href="http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx" rel="noreferrer">article</a> helped me come to this conclusion. I hope it is useful to you.</p>\n'}]
['https://Stackoverflow.com/questions/30310', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3085/']