asp.net mvc - Why do you need a route defined for Html.Action? -



asp.net mvc - Why do you need a route defined for Html.Action? -

i have created otherwise empty asp.net mvc 3 application 2 controllers, homecontroller , othercontroller.

homecontroller.cs looks this:

public class homecontroller : controller { public actionresult index() { homecoming view(); } }

index.cshtml looks this:

@html.action("index", "other")

and, of course, othercontroller.cs:

public class othercontroller : controller { [childactiononly] public actionresult index() { homecoming content("ok!"); } }

so far, good. run app, , tells me ok!

now, take default registerroutes global.asax.cs:

public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute( "default", // route name "{controller}/{action}/{id}", // url parameters new { controller = "home", action = "index", id = urlparameter.optional } // parameter defaults ); }

and crumple up, no routes match othercontroller:

public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.maproute("default", "", new { controller = "home", action = "index" }); }

now, when run page, crashes error message:

system.invalidoperationexception: no route in route table matches supplied values. source error: line 1: @html.action("index", "other")

i specified controller name , action name in phone call .action. no urls beingness generated, , no requests beingness made. why routing need involved?

i think blog post help understand little more:

http://blogs.charteris.com/blogs/gopalk/archive/2009/01/20/how-does-asp-net-mvc-work.aspx.

essentially, routing involved in determining controller 'fire up' handle request , appropriate action invoke based on parameters sent , mvcroutehandler uses create decision. because tell controller in action not create magically ignore routing table, go straight controller class , bypass other mvc goodness happens in back-end. remember, these @html.action methods can take whole lot of overloads influence route in routing table utilize (think url construction instance).

the mvc paths not static content , such, have parsed through urlroutingmodule uses routing table decide on do. since have no matching route - error.

edit

in diatribe, did not address final statement. you're right, no url generated, request application generated. html.action still utilize routing determine controller, action, area, parameters use. think fair in simple terms it's generating actionlink , clicking you.

asp.net-mvc routes

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -