在ASP.NET razor中处理.html为.cshtml

时间:2014-12-08 15:09:50

标签: html asp.net razor

是否可以在asp.net razor中处理.html文件,如.cshtml文件?

我想并排使用Webstorm和VS. (Webstorm用于html和js的东西,VS用于c#的东西)

1 个答案:

答案 0 :(得分:2)

默认情况下,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