获取ASP.NET MVC 4中的嵌套视图级别 [英] Get nesting level of view in ASP.NET MVC 4

查看:84
本文介绍了获取ASP.NET MVC 4中的嵌套视图级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种确定视图嵌套级别"的方法.我在stackoverflow.com上找到了确定视图嵌套级别" .但这仅适用于RenderAction,并且仅说明是否为子视图.

I've been searching for a way to determine the "nesting level" of a view. I've found: Determine view 'nesting level' here on stackoverflow.com. But that only works with RenderAction and only says if it is a child view or not.

我想要的是布局的级别为0,在布局中渲染的视图(例如,使用@RenderBody())具有级别1,在该视图中渲染的视图(例如,使用@Html.Partial(...))的视图具有级别2.

What I would like is that layout has level 0, views rendered in layout (e.g. with @RenderBody()) has level 1, views rendered in that view (e.g. with @Html.Partial(...)) has level 2.

例如:

  • _Layout.cshtml(0)
    • _LoginPartial.cshtml(1)
    • Index.cshtml(1)
      • DataTable.cshtml(2)
        • DataHeader.cshtml(3)
        • DataRow.cshtml(3)
        • _Layout.cshtml (0)
          • _LoginPartial.cshtml (1)
          • Index.cshtml (1)
            • DataTable.cshtml (2)
              • DataHeader.cshtml (3)
              • DataRow.cshtml (3)

              有人对此有解决方案吗?

              Do anyone have a solution for this?

              推荐答案

              经过一番调查,我发现了一个静态类System.Web.WebPages.TemplateStack,该类在执行视图时使用,在执行前将模板压入堆栈,执行后弹出,因此堆栈可用于确定级别.没有计数变量或任何公共属性/方法来获取实际的堆栈.但是,有一个私有方法GetStack(HttpContextBase).

              After some investigation I found a static class System.Web.WebPages.TemplateStack that is used when executing views, pushing template on to stack before execution and popping after execution so the size of the stack can be used to determine the level. There is no count variable or any public property/method to get the actual stack. However there is a private method GetStack(HttpContextBase).

              我通过使用反射和扩展方法解决了它:

              I solved it by using reflection and a extension method:

              using System;
              using System.Collections.Generic;
              using System.Linq;
              using System.Web;
              using System.Web.Mvc;
              using System.Web.WebPages;
              using System.Reflection;
              using System.Collections;
              
              namespace Mollwe.Helpers
              {
                  public static class TemplateStackLevelAccessor
                  {
                      private static MethodInfo _getStackMethod;
              
                      public static int GetTemplateLevel(this HtmlHelper htmlHelper)
                      {
                          return GetTemplateLevel(htmlHelper.ViewContext);
                      }
              
                      public static int GetTemplateLevel(this ViewContext viewContext)
                      {
                          return GetTemplateLevel(viewContext.HttpContext);
                      }
              
                      public static int GetTemplateLevel(this HttpContextBase httpContext)
                      {
                          if (_getStackMethod == null)
                          {
                              _getStackMethod = typeof(TemplateStack).GetMethod("GetStack", BindingFlags.NonPublic | BindingFlags.Static);
                          }
              
                          var stack = _getStackMethod.Invoke(null, new object[] { httpContext }) as Stack<ITemplateFile>;
              
                          return stack.Count - 1;
                      }
                  }
              }
              

              也许不是最好的方法,但是它可以工作.由于堆栈是在视图执行期间使用的,因此它将仅在视图或从视图调用的代码中使用.

              Maybe not the best way but it works. As the stack is used within execution of view it will only work in views or in code called from views.

              取决于System.Web.WebPages.WebPageBaseExecutePageHierarchy()的实现,该实现在RazorView.RenderView(...)中使用的派生类型System.Web.Mvc.WebViewPage中调用.

              Dependant on System.Web.WebPages.WebPageBase's implementation of ExecutePageHierarchy() that is called in derived type System.Web.Mvc.WebViewPage which is used in RazorView.RenderView(...).

              这篇关于获取ASP.NET MVC 4中的嵌套视图级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆