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 });
4

1 回答 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
}
于 2012-12-20T04:16:50.673 回答