Reputation: 2876
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 });
Upvotes: 3
Views: 3988
Reputation: 12358
I was able to get do something similar to this like this
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);
public ActionResult Question(string requestId)
{
ViewData["value"] = requestId;
return 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 of how the links appear with this route, I defined the
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
}
Upvotes: 2