Search code examples
asp.net-mvcurl-routing

Why does the URL change when I create a route?


I have an ASP.NET MVC5 project, and in a view, I have a link like this...

<a href="@Url.Action("Partner", "Home", new {id = Model.PartnerID})">
  ...loads of markup here...
</a>

This works fine, and gives me an URL of the form /Home/Partner/100

However, if I add a route for the action...

routes.MapRoute("Partner", "Partner", new { controller = "Home", action = "Partner" });

...then the URL is rendered as /Partner?id=100

Is there a way to get an URL of the form /Partner/100?


Solution

  • You can create your desired map route like following.

    routes.MapRoute(
            name: "Partner", 
            url: "{controller}/{action}/{id}", 
            defaults: new { controller = "Home", action = "Partner", id = UrlParameter.Optional }  
     );