3

I have a action like

 public class EmployerController : Controller
 {
 public ActionResult Message(string contractorId, int projectId)
 {....}

and a @html.ActionLink as

 @Html.ActionLink("Message", "Message", "Employer", new { contractorId = @model.ContractorId, projectId = @model.ProjectId }, null)

I end up with urls like

http://localhost:3597/Employer/Message?contractorId=contractor&projectId=10

whereas I want urls like

http://localhost:3597/Employer/Message/contractor/10

In my RouteConfig.cs, I have added

 routes.MapRoute(
            name: "MessageRoute",
            url: "Employer/Message/{contractorId}/{projectId}",
            defaults: new { controller = "Employer", action = "Message", contractorId = (string)null, projectId = 0 }
       );

What am I missing. How to disable querystring in routes?

/thanks.

Dandy
  • 2,177
  • 14
  • 16
  • I solved it by placing the custom routes.MapRoute method above the default routes.MapRoute method. – Dandy Jun 07 '13 at 22:03

2 Answers2

0

In your RouteConfig.cs, do you just have the route that you mentioned above in the post ("MessageRoute") or do you also have the Default one(below)? I am suspecting you could be having the following Default route registered before the "MessageRoute" one and thats the reason you are seeing this behavior.

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

If yes, you should flip the order of registration of these routes.

Kiran
  • 56,921
  • 15
  • 176
  • 161
0

You can generate Url by route url like this

<a href='@Url.RouteUrl("MessageRoute", new { contractorId = 6, projectId = 5})'>link</a>

and ur route

 routes.MapRoute(
        name: "MessageRoute",
        url: "Employer/Message/{contractorId}/{projectId}",
        defaults: new { controller = "Employer", action = "Message", contractorId = UrlParameter.Optional, projectId = UrlParameter.Optional }
   );

works for me result url is

/Employer/Message/6/5 

or if u need url like /Employer/Message/contraktor/6/project/5 just change the route to

        routes.MapRoute(
        name: "MessageRoute",
        url: "Employer/Message/contraktor/{contractorId}/project/{projectId}",
        defaults: new { controller = "Employer", action = "Message", contractorId = UrlParameter.Optional, projectId = UrlParameter.Optional }
   );
maxs87
  • 2,274
  • 2
  • 19
  • 23