- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下项目结构:
Solution
Project
Properties/
References/
Model/
Message.cs
Views/
Index.cshtml
EmailBuilder/
EmailBuilder.cs
Program.cs
我想从 Index.cshtml 文件中读取所有文本并将我的模型传递给该文件。但是,如果不设置 Copy to Output Directory: Copy if newer
,我无法从我的代码中打开 Index.cshtml。我不想将这些文件复制到输出目录,因为我不想让生成电子邮件的用户看到模板文件。这是我目前正在做的:
private static readonly string TemplateFolderPath =
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views");
RazorEngineManager.Instance.Razor.AddTemplate("Index",
File.ReadAllText(Path.Combine(TemplateFolderPath, "Index.cshtml")));
如何在不将其复制到输出目录的情况下读取 cshtml 文件?我来自 Java 世界,它就像从类路径解析文本或速度文件一样简单,不需要将文件复制到输出目录。这些文件保留在我的 jar 中。
我如何在 .NET 中执行此操作?
请帮忙。
最佳答案
.NET 中的一个类似技术(我犹豫说“等效”,因为我对 Java 的了解不够深,无法确定)是使用嵌入式资源。将 .cshtml 文件的生成操作设置为嵌入式资源,并使用 Assembly.GetManifestResourceStream
打开包含资源内容的流。
string resourceName = typeof(Program).FullName + ".Views.Index.cshtml";
using (Stream resourceStream = typeof(Program).Assembly.GetManifestResourceStream(resourceName))
{
// Read the contents
}
这假定 Program 类的命名空间是程序集的默认命名空间。通常情况下会出现这种情况,但如果您在项目创建后重命名了一些东西,它可能会不同步,因此请注意这一点。此外,如果找不到资源,流将为 null
,因此请务必检查一下。
您还可以使用 Assembly.GetManifestResourceNames
枚举模板。
string prefix = typeof(Program).FullName + ".Views.";
var templates = (from rn in typeof(Program).Assembly.GetManifestResourceNames()
where rn.EndsWith(".cshtml")
select new TemplateInfo
{
Key = Path.GetFileName(rn)
FileName = rn.Substring(prefix.Length)
ResourceName = rn
}).ToList();
现在您有了对象列表(您自己定义 TemplateInfo
),其中包含资源名称、文件名和可在模板管理器中使用的键。
这种技术有一个缺点:当您添加新的 CSHTML 文件时,您必须记住将其更改为 Embedded Resource。提示:如果您在 Visual Studio 中复制/粘贴该文件,它会将 Build Action 属性复制到新文件。
关于c# - .NET,C# : RazorEngine, 从项目文件夹中解析模板 CsHtml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53621756/
我第一次同时处理 ASP.NET、MVC 3、Web 开发。请耐心听我说,因为我知道这个主题已经从不同角度进行了大量讨论。我仍然没有找到我的具体问题的答案:为什么我的应用程序在出现 404 错误时找不
_PageStart.cshtml 与 _AppStart.cshtml 有什么区别?我可以互换使用它们吗? 最佳答案 _AppStart.cshtml 在应用程序第一次启动时执行一次。它是设置全局变
我有一个 DateTime 和 Date 的编辑器模板。 Date 的内容是 DateTime 的简单复制和粘贴。这是糟糕的复制粘贴编码。 对于用[DataType(DataType.Date)]修饰
当我在 IIS 8 中调用 .cshtml 页面时出现以下错误 “/”应用程序中的服务器错误。不提供此类页面。说明:您所请求的页面类型未被提供,因为它已被明确禁止。扩展名“.cshtml”可能不正确。
当我在 IIS 8 中调用 .cshtml 页面时出现以下错误 “/”应用程序中的服务器错误。不提供此类页面。说明:您所请求的页面类型未被提供,因为它已被明确禁止。扩展名“.cshtml”可能不正确。
每次我单击以创建 View 或部分文件时,Visual Studio 都会自动创建一个 _Layout.cshtml 和一个 _ViewStart.cshtml 文件。 我不希望我的项目创建 _Vie
我知道您可以对带有 url 的普通页面使用布局过滤器,但在出现错误时 View 会发生变化,同时保持 url 不变,因此我无法使用基于路径的新布局。任何帮助,将不胜感激。谢谢! 最佳答案 在我的脑海中
我无法在 mvc 3 razor 的 cshtml 文件中使用 RoleEnvironment.CurrentRoleInstance.Id。 RoleEnvironment 的错误表明它在当前上下文
我知道如果那是条蛇它会咬我的。但我不明白为什么它在 _Layout 头文件中有效,但在我需要它的特定索引页面中却无效。我看不到在每个使用 _Layout.cshtml 的页面中都有此代码。 这是 IF
我有一个分为多个区域的 ASP.NET MVC 4 站点。每个区域都有一个Views/Shared/_Layout.cshtml引用公共(public)共享布局的 View 。在通用布局中,我有一个侧
问题 其实有两个相关的问题: 我应该为每个页面创建一个 ViewModel 吗? 如果您在为两个页面(Create.cshtml 和 Edit.cshtml)创建单个 ViewModel 类时没有问题
在 ASP.Net 5 项目中,默认情况下我有一个名为 _ValidationScriptsPartial.cshtml 的文件:
这里有人可以帮助我吗?我有以下代码: @inherits umbraco.MacroEngines.DynamicNodeContext @{ var node = @Model.NodeBy
我希望有两个_Host.cshtml。我的想法是,我有一个具有非常不同的必需 css 和 js 的登录页面,以及一个具有不同必需的 css 和 js 的主/仪表板页面,我不想在单个 _Host 中加载
我尝试在我的 cshtml 页面中添加一个条件标签 var isTrendClicked = false; function trendChart() { $('.sparkline1'
我有一个简单的选择列表项,被插入下拉列表中。 这是 HTML 结果 Options Option1 Option2 基本上我有这个简单的 JavaScript 脚本 $(document).read
按照 MVC 中的教程,你能告诉我在 ASPX 页面中的替代方法吗 在 cshtml 页面中,VS2013 上 MVC 4 中的默认 View 类型。当我尝试上述时,显示的是字面意思。即
我想知道是否有人测试了他们基于 MVC .NET 的 Web 服务的图形用户界面部分。我用过 Watin/Specflow。我对此有两个主要顾虑: 这些测试不仅仅是图形用户界面。它们是系统检查。他们测
我正在为工作更新 Intranet 站点,需要知道如何水平对齐列表项。我知道这是通过 CSS 完成的,但我在弄清楚使它保持水平的错误所在时遇到了一些问题。 _SiteLayout.cshtml
如果特定列包含重复条目,如何清除 HTML 表格的记录?例如,如果我的表中有 2 列 - 姓名和年龄,如果 2 个人具有相同的年龄,则完全删除第二行。 function DeleteDuplicat
我是一名优秀的程序员,十分优秀!