Reputation: 106620
I have a partial page containing the following javascript:
var url = '@Url.Action("Details", "Users", null)/' + userID;
This works fine on every page, except for pages with the url structure:
/Site/Users/Details/{ID}
For example when ID = 25, asp.net will output:
var url = '/Site/Users/Details/25/' + userID;
But it should be:
var url = '/Site/Users/Details/' + userID;
How can I prevent @Url.Action
from assuming this additional route value?
Edit:
My route contains the default route configuration...
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Upvotes: 4
Views: 301
Reputation: 133403
Try something like this
var url = '@Url.Action("Details", "Users", new { id = -1})'; //Render Url with -1 value
url = url.replace(-1, userID); //replace -1 with userId
Upvotes: 2
Reputation: 17166
Maybe try something like:
var urlString = Url.Action("Details", "Users", null, "http");
var uri = new Uri(urlString);
and in javascript:
var url = '@uri.AbsolutePath' + '/' + userID;
Upvotes: 0
Reputation: 3062
Why not include the id in route values for the Url Action method?
var url = '@Url.Action("Details", "Users", new {id = userID})';
Upvotes: 0