c# - Razor 部分名称的提示/流利?

标签 c# asp.net-mvc

所以我有一个案例,布局已经变得更加复杂。有一些常见的东西,比如 @section styleIncludes{ ... } ,然后是其他部分,这些部分定义了每个页面可以选择(但几乎总是)指定的各种内容,例如当前页面面包屑的结构。所有这些东西都是部分的原因是因为它们嵌入在布局结构中。

我发现自己在复制之前的页面,因为有 8 个左右不同的部分,而不是试图记住它们的确切拼写,或者零碎地复制/粘贴。

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

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

但是,razor 中的扩展/助手返回 MvcHtmlString,我想 @section 由完全不同的东西表示。

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

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

您建议流式方法的参数类型是什么,例如 Style 或 Breadcrumb?我希望传递的 razor 与在部分声明的花括号中编写 razor 的能力相似。例如,能够访问在 razor 页面上声明的局部变量,就像您可以使用常规部分声明一样。我不想要像 .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
}

我宁愿拥有像这样的 seom 类流畅的 API,并不是真正为了最终的代码风格,而是因为它会更容易编写代码并且不会出现拼写错误等问题。因为智能感知会提供帮助:

@{
  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>)

最佳答案

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

首先,我们必须了解 MVC 的工作原理。我们编写cshtml文件,但是这些文件最终会编译成一个.Net class实例化然后执行方法(至少 Execute() )(大部分时间)写入响应缓冲区供 IIS 返回(与 RenderAction 的工作方式即使不完全相同也非常相似 - 调用子操作方法并在父 View 中内联呈现结果)。 HtmlHelpers(或任何自定义助手)只是从实例化类(作为委托(delegate))中调用,因此它们只能在编译 cshtml 文件中的代码后执行。

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

可以做的是编写您自己的 HtmlHelper 或其他自定义帮助程序来执行类似于 Sections 提供的功能(无需实际使用任何 sections)。例如I wrote this because I needed to write Javascript from partial views and/or templates (which you can't do with sections) .如果您需要使用部分,那么这可能无济于事。

以下代码仅作为示例,它并不是 Razor 的实际工作方式(根本不是在查看一些代码之后)。但为了使这个示例有意义,我将使用像 Razor 一样的代码和命名约定。

布局.cshtml

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

@RenderBody();
</body>
</html>

索引.cshtml

@{
  _layout = "layout";
}

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

<div>My Body</div>

可能会被编译成类似于以下内容的类:

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>");
  }
}

关于c# - Razor 部分名称的提示/流利?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16490069/

相关文章:

c# - Linq 从表中选择 *

c# - 不移动光标修改文本框

asp.net-mvc - ASP MVC 4 不处理静态文件

asp.net - VB.Net - 将 WebApi 5 添加到现有 MVC 5 站点

c# - 尝试恢复数据库中列的当前值时出错

c# - 什么是{get;放; } C# 中的语法?

asp.net - 模型-存储库-服务-验证器- View - View 模型- Controller 设计模式(?)

c# - Entity Framework 用户和角色管理

.net - 如何强制硬刷新 (ctrl+F5)?

C# 属性 XmlIgnore 和 XamlWriter 类 - XmlIgnore 不工作