Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Route matching for child actions

Does the approach of route matching for child actions differ from usual actions? In other words, do child actions have some autogenerated url to make matching similar to what is done for parent actions?

like image 254
SiberianGuy Avatar asked Jul 29 '11 04:07

SiberianGuy


1 Answers

No difference between parent or child action processing

Any action follows the same route definition you've set in your Application_Start. That means parent actions as well as child ones. If you gave specific routes for all actions in your application, then you must provide route definitions for your child actions as well.

Avoid route processing by converting to Html.RenderPartial()

If you can of course... Child actions go through the same MVC processing as parent actions. It's of course different if you can change your Html.RenderAction() to Html.RenderPartial(). These don't go through the same processing hence are much much faster. Use Html.RenderAction() only when you can't do it any other way or doing it in other way would be to hackish (increased complexity of the view's model type etc).

Actual Html.RenderAction() code excerpt

If you look at the code of Html.RenderAction() it calls into context processing to execute as if a request was made to the server:

// other code before this
RouteData routeData = CreateRouteData(data.Route, routeValues, data.DataTokens, htmlHelper.ViewContext);
HttpContextBase httpContext = htmlHelper.ViewContext.HttpContext;
RequestContext context = new RequestContext(httpContext, routeData);
ChildActionMvcHandler httpHandler = new ChildActionMvcHandler(context);
httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(httpHandler), textWriter, true);

We can se that it uses ChildActionMvcHandler handler which is inherited from MvcHandler, but is basically no different from it in terms of execution, because it doesn't have any new or changed functionality related to processing. So it executes MvcHandler's code basically.

Outcome?

Child actions execute as parent actions, using the same routing definitions, controller action matching (action method selectors), filters etc.

like image 167
Robert Koritnik Avatar answered Nov 17 '22 09:11

Robert Koritnik