Qua*_*els 2 asp.net-mvc asp-classic
我一直在使用经典ASP开发大约2.5年,我正在尝试更新我的技能集以包含ASP.NET MVC.
什么是MVC执行SSI的方式?IE:如何在侧边栏中包含数据库绘制的导航列表?我一直在研究部分视图,但他们似乎从控制器获取了他们的内容.据我所知,这意味着我需要编写每个控制器来传递导航列表.
我在思考正确的方向吗?
作为使用RenderAction()的替代方法(由于它必须运行整个ASP.NET请求管道来获取其输出,因此它有一些缺点),您可以使用所有控制器继承的BaseController类型,它覆盖OnActionExecuted()以插入值进入ViewData集合.使用这种方法,您将获得使用单个请求的好处,并且无需担心每次处理请求时都会将交叉数据手动添加到模型中.
为了简单起见,我喜欢public const string SomeDataItemViewDataKey = "Controller.DataName";在控制器类定义中使用a 来键入控制器添加的ViewData条目,然后在视图中当我需要渲染输出时,我可以使用模板化助手来从ViewData中提取值:<%=Html.DisplayFor(ControllerType.SomeDataItemViewDataKey, "PartialViewUsedToRenderTheData") %>.
因为我的陈述的有效性存在一些混淆,这里是针对RenderAction()的性能声明的原始来源:
是的,RenderAction(较慢)与RenderPartial(更快)的性能存在显着差异.根据定义,RenderAction必须运行整个ASP.NET管道来处理系统看起来是新HTTP请求的内容,而RenderPartial只是向现有视图添加额外内容.
-Brad Wilson,ASP.NET MVC团队的高级开发人员
引用来源:http://forums.asp.net/p/1502235/3556774.aspx#3556590
Brad Wilson的博客:http://bradwilson.typepad.com/
这是来自MVC2 RTM源中的RenderAction()的代码,我们可以看到,虽然有一个新的请求被触发,但它实际上不再通过整个ASP.NET管道了.话虽如此,在PartialViews和ViewModel/ViewData的替代方案中使用它仍然有一些微小的但通常可以忽略不计的缺点.根据我的理解(现在),在从MvcFutures程序集移动到核心框架之前,对RenderAction()的实现进行了彻底检查; 所以它可能认为布拉德威尔逊上面的声明在他6个月前制作时比现在更有效.
internal static void ActionHelper(HtmlHelper htmlHelper, string actionName, string controllerName, RouteValueDictionary routeValues, TextWriter textWriter) {
if (htmlHelper == null) {
throw new ArgumentNullException("htmlHelper");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
routeValues = MergeDictionaries(routeValues, htmlHelper.ViewContext.RouteData.Values);
routeValues["action"] = actionName;
if (!String.IsNullOrEmpty(controllerName)) {
routeValues["controller"] = controllerName;
}
bool usingAreas;
VirtualPathData vpd = htmlHelper.RouteCollection.GetVirtualPathForArea(htmlHelper.ViewContext.RequestContext, null /* name */, routeValues, out usingAreas);
if (vpd == null) {
throw new InvalidOperationException(MvcResources.Common_NoRouteMatched);
}
if (usingAreas) {
routeValues.Remove("area");
}
RouteData routeData = CreateRouteData(vpd.Route, routeValues, vpd.DataTokens, htmlHelper.ViewContext);
HttpContextBase httpContext = htmlHelper.ViewContext.HttpContext;
RequestContext requestContext = new RequestContext(httpContext, routeData);
ChildActionMvcHandler handler = new ChildActionMvcHandler(requestContext);
httpContext.Server.Execute(HttpHandlerUtil.WrapForServerExecute(handler), textWriter, true /* preserveForm */);
}
Run Code Online (Sandbox Code Playgroud)