3

I have a controller called Quote, with an Index method that requires a requestId parameter. The URL appears as such

/Quote/{requestId}. 

Additionally, I have a method called ApplicantInfo which is specific to the quote and routes as such

/Quote/{requestId}/ApplicantInfo. 

But when I use the Url.Action helper like so

@Url.Action("Index","Quote",new { requestId = {requestId}})

It gives me a url of

/Quote/ApplicantInfo?requestId={requestId} 

which does not route correctly.

Obviously, I can manually create the URL string, but before I throw in the towel I wanted to know if I was missing something, for instance an override to the Url.Action method that will output the correct Url.

TIA

ROUTES

routes.MapRoute(
            "QuoteInfo",
            "Quote/{requestid}", 
            new { controller = "Quote", action="Index" });

routes.MapRoute(
            "QuoteApplicant",
            "Quote/{requestid}/ApplicantInfo", 
            new { controller = "Quote", action = "ApplicantInfo" });

routes.MapRoute(
             "Default",
             "{controller}/{action}/{id}", 
     new { controller = "Home", action="Index", id = UrlParameter.Optional });
1
  • 1
    can you show me your RegisterRoutes method in RouteConfig if you do all your routings here? Commented Dec 19, 2012 at 22:04

1 Answer 1

2

I was able to get do something similar to this like this

Route

Define a route in Global.asax.cs or whereeve you override your routes

        routes.MapRoute(
            "Organization_default",
            "{controller}/{requestId}/{action}", 
            new { 
                  controller = "home", 
                  action = "index", 
                  requestId = "default",
                  id = UrlParameter.Optional 
             }, null);

Controller

    public ActionResult Question(string requestId)
    {
        ViewData["value"] = requestId;
        return View();
    }

View

@{
    ViewBag.Title = "Index";
    var value = ViewData["value"];
}

<h2>Stackoverflow @value</h2>
@Html.ActionLink("Click Here", 
        "question", "StackOverflow", new { requestId ="Link" }, new{   @id="link"})

Screenshot

screenshot of how the links appear with this route, I defined the enter image description here

Catch

You CANNOT have another route as {controller}/{action}/{key} defined before your custom this route. If you do the other route override your custom route.

If you need both the routes to co-exist then you would have to define a separate Area and define your custom route overriding RegisterArea

public override void RegisterArea(AreaRegistrationContext context)
{
   //..custom route definition
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this got me where I needed to be.

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.