ASP.NET MVC Route matching for child actions

后端 未结 1 2070
有刺的猬
有刺的猬 2021-02-15 15:45

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

相关标签:
1条回答
  • 2021-02-15 16:03

    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.

    0 讨论(0)
提交回复
热议问题