0

I have the following RouteLink:

@Html.RouteLink("Accept Offer", new { controller = "Case", action = "Accept", id = item.CaseId, offerid = item.PxOfferId }, new { @class = "btn btn-success" })

Which formats the URL as:

http://localhost:54644/Clients/Case/Accept/15847?offerid=3103

How do I format the URL to be:

http://localhost:54644/Clients/Case/Accept/15847/3103

Thanks.

Default route:

    context.MapRoute(
        "Clients_default",
        "Clients/{controller}/{action}/{id}",
        new { controller = "Login", action = "Index", id = UrlParameter.Optional }
    );

Route works if I put this first in my defined routes:

    context.MapRoute(
        name: "Accept Offer",
        url: "Clients/{controller}/{action}/{id}/{offerid}",
        defaults: new { controller = "Case", action = "Accept", id = 0, offerid = UrlParameter.Optional }
    );

but then causes errors on other pages.

4
  • 1
    You need to define a specific route with a placeholder for offerid Commented Apr 7, 2017 at 9:05
  • Try adding offerId on route configuration like this: {controller}/{action}/{id}/{offerid} and declare offerid as UrlParameter.Optional. Commented Apr 7, 2017 at 9:05
  • can you share the route definition you have in RouteConfig ? If you don't have one set up you'll need to do that. Then you will be able to achieve the url format your require Commented Apr 7, 2017 at 9:05
  • @scgough just added that info to the question. Commented Apr 7, 2017 at 9:35

1 Answer 1

1

You nee a Route in the RouteConfig file in App_Start (if you are using mvc3/4 then routes might be configured in the Global.asax file instead):

routes.MapRoute(
    name: "MyCaseRoute",
    url: "/clients/{controller}/{action}/{id}/{offerId}",
    defaults: new { controller = "Case", action = "Accept", id = UrlParameter.Optional, offerId = UrlParameter.Optional }
);

usage:

@Html.RouteLink("My Link Text", routeName: "MyCaseRoute", routeValues: new { controller = "case", action = "accept", id = 1, offerId = 2 })
Sign up to request clarification or add additional context in comments.

4 Comments

Only the last parameter can be marked UrlParameter.Optional and you should also note that this should be before any other matching routes
I'm using MVC 5. The route displays correctly if I put the above route first, but then this causes errors on every other page.
This is the route I put it above: context.MapRoute( "Clients_default", "Clients/{controller}/{action}/{id}", new { controller = "Login", action = "Index", id = UrlParameter.Optional } );
Ah, got it working. Thanks for the info, the error I was getting was because it was becuase of the default controller and action I had on the mapped route as the Accept action requires offerid parameter. your info helped me though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.