在ASP.NET MVC中,ASP classic的服务器端包含的是什么?

时间:2010-05-07 16:44:48

标签: asp.net-mvc asp-classic

我一直在使用经典ASP开发大约2。5年,我正在尝试更新我的技能集以包含ASP.NET MVC。

执行SSI的MVC方式是什么? IE:如何在侧边栏中包含数据库绘制的导航列表?我一直在研究部分视图,但他们似乎从控制器获取了他们的内容。据我所知,这意味着我需要编写每个控制器来传递导航列表。

我是否按照正确的思路思考?

4 个答案:

答案 0 :(得分:6)

作为使用RenderAction()的替代方法(由于它必须运行整个ASP.NET请求管道来获取其输出,因此有一些缺点),您可以使用所有控制器继承的BaseController类型覆盖OnActionExecuted()将值插入ViewData集合。使用这种方法,您将获得使用单个请求的好处,并且无需担心每次处理请求时都会手动将交叉切割数据添加到模型中。

为了简单起见我喜欢在控制器类定义中使用public const string SomeDataItemViewDataKey = "Controller.DataName";来键入控制器添加的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/

更新2

这是来自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 */);
}

答案 1 :(得分:4)

您有两个与SSI类似的选项

  1. 使用RenderAction或Action(explanation of the difference between the two
  2. 如果您正在使用MVC2,还有DisplayFor辅助方法,可以为特定对象定义自定义显示模板。
  3. 在您的情况下,选项1可能是更好的选项,因为您可能不希望在视图模型/视图数据中包含菜单项。

答案 2 :(得分:2)

是的,部分观点是要走的路。控制器应该处理表示逻辑。类似于Web表单和Web控件背后的代码。

我也会使用母版页来进行导航。

答案 3 :(得分:1)

如果我理解你的问题是正确的,这是你想要的每一页,RenderAction可能是你的朋友。