1

I am working on an ASP.NET MVC app. In my app, I have a controller that looks like this:

[RoutePrefix("account/orders")]
public partial class AccountOrdersController : Controller
{
  [Route]
  public virtual ActionResult Index()
  {
    return View();
  }

  [Route("add")]
  [Route("~/dashboard/orders/{orderId}/item")]
  public virtual ActionResult Add(string orderId)
  {
    return View();
  }
}

In the Index view, I am referencing the Add action like this:

var url = '@Url.Action("Add", "AccountOrdersController", new RouteValueDictionary(new { orderId = "xyz" }))';

When the view gets rendered, the above becomes:

var url = '/account/orders/add?orderId=xyz';

How do I get the above to be rendered as:

var url = '/account/orders/xyz/item';

I'm trying to create a URL that is a complete path instead of just appending query string parameters.

2 Answers 2

1

When applying multiple routes your can set the order the route is applied using the Order property on the [Route] attribute. By default, all defined routes have a Order value of 0 and routes are processed from lowest to highest.

[Route("add", Order = 2)]
[Route("~/dashboard/orders/{orderId}/item", Order = 1)]
public virtual ActionResult Add(string orderId){...}
Sign up to request clarification or add additional context in comments.

Comments

0

MVC always short-circuits routes. The /account/orders/add route is perfectly valid, since the query string is arbitrary: it can just stuff whatever was leftover into that. If you want a more complex route on an action that has a simpler route, then your only option is to name the route and get it specifically by name:

[Route("~/dashboard/orders/{orderId}/item", Name="DashboardOrderAdd")]

Then:

@Url.RouteUrl("DashboardOrderAdd", new { orderId = "xyz" })

Comments

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.