【问题标题】:Process .html in ASP.NET razor as .cshtml在 ASP.NET razor 中将 .html 处理为 .cshtml
【发布时间】:2014-12-08 15:09:50
【问题描述】:

asp.net razor 可以像.cshtml 文件一样处理.html 文件吗?

我想同时使用 Webstorm 和 VS。 (Webstorm 用于 html 和 js 的东西,VS 用于 c# 的东西)

【问题讨论】:

  • 不能 webstorm 处理 cshtml?!或者你不能设置一个标志说把cshtml和html一样对待吗?!
  • 定义“过程”。你能展示你希望如何使用它的相关代码吗?
  • 按进程我的意思是执行包含的内联c#代码。 Webstorm 不解析 .cshtml
  • 仍不清楚。你的意思是在HomeControllerIndex()方法中你想要return View();,应该渲染~/Views/Home/Index.html
  • 是的。它还应该处理 html 中的内联 c# 代码

标签: html asp.net razor


【解决方案1】:

默认情况下,Razor 引擎仅查找 .cshtml.vbhtml 文件。您可以通过定义继承自 RazorViewEngine 的自定义 ViewEngine 并添加位置来搜索视图来更改此行为。

来自Make ASP.NET MVC 3 Razor View Engine ignore .vbhtml files

public class CSRazorViewEngine : RazorViewEngine {

    public CSRazorViewEngine() {

        base.AreaViewLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.html",
        };

        base.AreaMasterLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.html",
        };

        base.AreaPartialViewLocationFormats = new string[] { 
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.html",
            "~/Areas/{2}/Views/Shared/{0}.html",
        };

        base.ViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.html",
            "~/Views/Shared/{0}.html",
        };

        base.PartialViewLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.html",
            "~/Views/Shared/{0}.html",
        };

        base.MasterLocationFormats = new string[] {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/{1}/{0}.html",
            "~/Views/Shared/{0}.html",
        };

        base.FileExtensions = new[]
        {
            "cshtml",
            "html",
        };
    }
}

然后在你的Application_Start()注册:

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CSRazorViewEngine());

或者只是add .cshtml to your Webstorm's configuration as File Type HTML

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2013-02-05
    • 2014-07-21
    • 1970-01-01
    • 2018-03-27
    • 2021-05-19
    相关资源
    最近更新 更多