ASP.NET MVC Routing and URL.Action

in one of my asp.net mvc 2 views I have the following statement

window.location.href = '<% = Url.Action("Index","Feature", new {id=""}) %>/' + $("#ProductId").val(); 

as you can see $ ("# ProductId"). val () can only be computed from a client action and therefore outside of url.Action

I have my routes as shown below:

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" }); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.IgnoreRoute("{*allimages}", new {allimages = @".*\.jpg(/.*)?"}); routes.MapRoute( "Default", // Route name "{controller}.mvc/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "DefaultIndex", // Route name "{controller}.mvc/{id}", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); routes.MapRoute("Root", "", new { controller = "Home", action = "Index", id = "" }); } 

The request with Url.Action does not work, because the route believes that the "action" is "id" enter image description here

How can I make sure the route setting in "DefaultIndex" is checked and the URL is

Key → Value
controller = Feature
action = index
id = 9a1347dc-60b0-4b3b-9570-9ed100b6bc6a

Edit 2

Image 2:

enter image description here

Change 1- Route Order

I almost decided that I solved it by changing the order of the route

  routes.MapRoute( "DefaultIndex", // Route name "{controller}.mvc/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute( "Default", // Route name "{controller}.mvc/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); routes.MapRoute("Root", "", new { controller = "Home", action = "Index", id = "" }); 

it really worked http: // localhost: 61000 / Feature.mvc / 9a1347dc-60b0-4b3b-9570-9ed100b6bc6a /

but failed for posting on the same page

http: // localhost: 61000 / Product.mvc / List

**** History ****

Used + $ ("# ProductId"). val (); = "/ domainname [virtual directory alias] /Fewature.mvc/Index" + $ ("# ProductId"). val ();

always worked, although I had to change all the scenarios when sending to the domainname server [virtual directory alias] / changes from development to production testing

trying to arrange it:

The following statement used to be:

window.location.href = '<% = Url.Action ("Index", "Feature"})%> /' + $ ("# ProductId"). val ();

The result will be several Id values

http: // localhost: 61000 / Feature.mvc / 9a1347dc-60b0-4b3b-9570-9ed100b6bc6a / 3c5941e4-cb25-4d6f-9117-9ed100b4ca91

it maps the existing route {id} in Url.Action ("Index", "Feature"}

bringing NoMatch

Submitted by

new {id = ""} to get around it.

+4
source share
2 answers

Try using Url.RouteUrl instead

 window.location.href = '<%= Url.RouteUrl("Default", new { @Controller = "Feature", @Action = "Index"}) %>/' + $("#ProductId").val(); 
+2
source

Your routing is messed up. What is the reason for the second route: {controller}.mvc/{id} ...? He is facing the first route.

If a URL like /mycontroller/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a comes into the routing mechanism, it always directs it to {controller}/{action}/{id} because it is first on the list, and the URL -address can be mapped to this route, that is, there are no route restrictions, and the identifier is optional.

If I were you, I would just delete the second route ... if you really need a second route, then move it above the first route, and then put a route restriction on it.

+1
source

All Articles