提示/流利的剃刀部分名称?

Aar*_*nLS 11 c# asp.net-mvc

所以我有一个案例,布局已经发展变得更加复杂.有通常的事情@section styleIncludes{ ... },然后其他部分定义了每个页面可以选择(但几乎总是)指定像当前页面痕迹的结构的各种事物.所有这些都是部分的原因是因为它们嵌入在布局的结构中.

我发现自己制作了以前页面的副本,因为有大约8个不同的部分,而不是试图记住它们的确切拼写,或者是单独复制/粘贴.

我认为最好为这些创建一个流畅的API,以便我有一些具有8个函数的对象,每个函数都返回对象本身,所以你可以做一些像Sections.Style(一些MVC文本模板或razor delgate) ?).面包屑(等)

主要目的是能够以引导的方式对这些部分进行编码,并强烈键入名称,而不是依赖于完美的键入或复制/粘贴.

然而,剃刀中的扩展/帮助器返回MvcHtmlString,我想@section由完全不同的东西表示.

不是要求你为我写一个完整的解决方案,而只是提出一些关于如何先行的想法.

助手返回什么对象来表示@section声明? 即MvcHtmlString的类比.

你会建议什么样的流畅方法的参数类型,如Style或Breadcrumb? 我希望剃刀传递的能力类似于在部分声明的花括号中编写剃刀.例如,能够访问剃刀页面上声明的局部变量,就像使用常规部分声明一样.我不希望像字符串连接之类的东西.SomeSection("<div...>Bunch of html stuffed in a string</div>")

换句话说,如果我的许多cshtml页面开始像

@{
  string title = "Edit Person"
  ViewBag.Title = title;
}
@section styles{
  .someOneOffPageSpecificStyle { width:59px }
}
@section javascript{
  //javascript includes which the layout will place at the bottom...
}
@section breadcrumb{
  <a ...>Parent Page</a> &gt; <a ...>Sub Page</a> &gt; @title
}
Run Code Online (Sandbox Code Playgroud)

我宁愿像这样有一些流畅的API,不是真正的代码风格,而是因为它更容易编写代码而不会出现错别字等问题,因为intellisense会帮助:

@{
  string title = "Edit Person"
  ViewBag.Title = title;
}
@Sections
.Styles(@<text>
  .someOneOffPageSpecificStyle { width:59px }
</text>)
.Javascript(@<text>
  //javascript includes which the layout will place at the bottom...
</text>)
.Breadcrumb(@<text>
  <a ...>Parent Page</a> &gt; <a ...>Sub Page</a> &gt; @title
</text>)
Run Code Online (Sandbox Code Playgroud)

Eri*_*ips 2

这很可能是不可能的(使用部分)。

首先我们必须了解 MVC 在底层是如何工作的。我们编写 cshtml 文件,但这些文件最终将被编译成实例化的.Net 类,然后执行方法(至少是Execute()),该方法(大多数时候)写入响应缓冲区以供 IIS 返回(与RenderAction 的工作方式非常相似,即使不完全相同-调用子操作方法并在父视图中内联渲染结果)。HtmlHelpers(或任何自定义帮助程序)只是从实例化类(作为委托)调用,因此它们只能在编译 cshtml 文件中的代码后执行。

System.Web.Razor.Generator.SectionCodeGenerator 需要字符串定义来创建节。因此,当您定义一个部分时,在编译文件之前,字符串必须存在于 cshtml 文件中,因为在编译文件之前,不会执行 HtmlHelper 或/和自定义帮助程序,因此没有办法编写这样的类或对象:可以在编译之前更新 cshtml 文件。

可以做的是编写自己的 HtmlHelper 或其他自定义帮助程序来执行类似于部分提供的操作(而不实际使用任何部分)。例如,我写这个是因为我需要从部分视图和/或模板(你不能用部分来做到这一点)编写Javascript。如果您需要使用部分,那么这可能没有帮助。

以下代码只是示例,它并不是 razor 的实际工作原理(在查看了一些代码之后)。但为了使这个示例有意义,我将使用像剃刀一样的代码和命名约定。

布局.cshtml

<html>
<body>
@RenderSection("MySectionName")

@RenderBody();
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

索引.cshtml

@{
  _layout = "layout";
}

@section MySection {
  <div>MySection</div>
}

<div>My Body</div>
Run Code Online (Sandbox Code Playgroud)

也许会被编译成一个类似于以下内容的类:

public class app_aspnet_layout : System.Web.Mvc.WebViewPage
{

  public Execute()
  {
    throw new NotImplementedException();
  }

  public void ExecutePageHierarchy(WebPageContext pageContext, 
                                   TextWriter writer)
  {
    writer.Write("<html>")
    writer.Write("<body>")

    var section = pageContext.SectionWriters["MySectionName"];
    section();

    pageContext.View.ExecutePageHierarchy(null, writer)

    writer.Write("</body>")
    writer.Write("</html>")
  }
}

public class app_aspnet_index : System.Web.Mvc.WebViewPage
{ 
  // generated from the _layout Definition
  private WebViewPage startPage = new app_aspnet_layout();

  public Execute()
  {
     WebPageContext pageContext = new WebPageContext();
     pageContext.View = this;

     pageContext.SectionWriters.Add("MySectionName", 
                                    this.Section_MySectionName);

     var writer = HttpContext.Current.Response.Stream.AsTextWriter();

     if (startPage != null)
     {
       startPage.ExecutePageHierarchy(pageContext, writer);
     }
     else
     {
       this.ExecutePageHierarchy(pageContext, writer);
     }
  }

  // html generated from non-section html
  public void ExecutePageHierarchy(WebPageContext pageContext, 
                                   TextWriter writer)
  {
    writer.Write("<div>My Body</div>");
  }

  public void Section_MySectionName(TextWriter writer)
  {
    writer.Write("<div>MySection</div>");
  }
}
Run Code Online (Sandbox Code Playgroud)