Url.Action assumes additional route binding

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 a 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 accepting this extra 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 } ); } 
+4
source share
3 answers

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 
+2
source

Why not include the identifier in the route values ​​for the Url Action method?

 var url = '@Url.Action("Details", "Users", new {id = userID})'; 
0
source

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; 
0
source

Source: https://habr.com/ru/post/1494876/


All Articles