ASP.NET MVC Routing and URL.Action?

Your routing is all messed up. What's the reason for the second route: {controller}. Mvc/{id} ...? It's clashing with the first route.

Up vote 1 down vote favorite share g+ share fb share tw.

In one of my asp.net mvc 2 views I am have the following statement window.location. Href = '/' + $("#ProductId").val(); as can be seen $("#ProductId").val() can only be computed from client action and so outside 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 fails because the route thinks the "action" is the "id" How can I make sure the route configuration in "DefaultIndex" is validated and the url is Key >> Value controller = Feature action = index id = 9a1347dc-60b0-4b3b-9570-9ed100b6bc6a Edit 2 Image 2: Edit 1- Route Order I almost thought I solved it by changing the route order 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 did work for http://localhost:61000/Feature.

Mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/ but failed for a post on same page http://localhost:61000/Product. Mvc/List History ** Have been using + $("#ProductId").val(); ="/domainnamevirtual directory alias/Fewature. Mvc/Index"+$("#ProductId").val(); always worked, though I had to change all scripts when posted to server domainnamevirtual directory alias/ changes from development to test to production trying to streamline it: Had the following statement earlier: window.location.

Href = '/' + $("#ProductId").val(); Would result in multiple Id values http://localhost:61000/Feature. Mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/3c5941e4-cb25-4d6f-9117-9ed100b4ca91 it maps existing route {id} into Url. Action("Index","Feature"} resulting in NoMatch Introduced new {id=""} to get around it.

Asp. Net-mvc-2 asp.net-mvc-routing link|improve this question edited Sep 20 '11 at 19:32 asked Sep 20 '11 at 16:35TheMar284220 67% accept rate.

Came up with a hack but I don't want to use it-- Idea is to introduce a "/" and then Url. Action renders action ("index")window.location. Href = '' + $("#ProductId").val(); – TheMar Sep 20 '11 at 21:23.

Your routing is all messed up. What's the reason for the second route: {controller}. Mvc/{id} ...? It's clashing with the first route.

If a URL like /mycontroller/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a comes in the routing engine will always route it to {controller}/{action}/{id} because it's first in the list and the url can be mapped to that route, i.e. There are no route constraints and id is optional. If I was you, I would just remove the second route... if you really need the second route then move it above the first route and then put a route constraint on it.

Charlino- I tried moving the 2nd route to the top. It worked as I said in my edit 1 but somehow my JQGrids post started to fail. On the constraints , is it possible that if url is like /mycontroller/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a and the last part conforms to Guid to have it mapped to {Id} and have action defaulted to "Index" – TheMar Sep 20 '11 at 17:20 Yes, that's possible.

But you're making things hard for yourself... why don't you just stick with the one route {controller}/{action}/{id}? – Charlino Sep 20 '11 at 17:28 @Charlino- I would like to. But /' + $("#ProductId").val(); evaluates to Feature.

Mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a >> Routing takes action =id – TheMar Sep 20 '11 at 19:03 @charlino- checkout Image 2 under Edit 2 in my question. If I make use of default routing, url. Action evaluates to Feature.

Mvc/Feature. Mvc/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a. If it had the index, the url will work.

Anyway to force Index to show up in Url. Action if {id} is empty or null – TheMar Sep 20 '11 at 19:35.

Try to use Url. RouteUrl instead window.location. Href = '/' + $("#ProductId").val().

The problem with double ids in this case-- localhost:61000/Feature. Mvc/Index/… when I try to search the same id again – TheMar Sep 20 '11 at 17:08 it seems to have truncated the url. Here it is again Feature.

Mvc/Index/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a/9a1347dc-60b0-4b3b-9570-9ed100b6bc6a – TheMar Sep 20 '11 at 17:10 can you check the value from $("#ProductId").val()? The Url. RouteUrl part shouldn't generate the link with id.

– Henry Oh Sep 20 '11 at 17:45 it does. It is I think a feature of ASP. NET MVC routing.

Look at stackoverflow.com/questions/5734679/… stackoverflow.com/questions/2088605/… – TheMar Sep 20 '11 at 18:58 can you post me what '/' renders? – Henry Oh Sep 20 '11 at 21:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions