您可以基于 Razor 视图引擎编写自定义视图引擎并为您的视图注册自定义扩展:
public class VXMLViewEngine : RazorViewEngine
{
public VXMLViewEngine()
{
ViewLocationFormats = new[] { "~/Views/{1}/{0}.vxml", "~/Views/Shared/{0}.vxml" };
MasterLocationFormats = new[] { "~/Views/{1}/{0}.vxml", "~/Views/Shared/{0}.vxml" };
PartialViewLocationFormats = new[] { "~/Views/{1}/{0}.vxml", "~/Views/Shared/{0}.vxml" };
FileExtensions = new[] { "vxml" };
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
controllerContext.HttpContext.Response.ContentType = "text/vxml";
return base.CreateView(controllerContext, viewPath, masterPath);
}
}
并在 Application_Start 中声明自定义扩展:
RazorCodeLanguage.Languages.Add("vxml", new CSharpRazorCodeLanguage());
WebPageHttpHandler.RegisterExtension("vxml");
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new VXMLViewEngine());
以及 web.config 中将识别.vxml
扩展的自定义构建提供程序:
<compilation debug="true" targetFramework="4.0">
<assemblies>
...
</assemblies>
<buildProviders>
<add extension=".vxml" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</buildProviders>
</compilation>
现在您可以.vxml
用作视图的扩展。由于 Visual Studio 无法识别.vxml
扩展名,因此当您使用它打开文件时,它不会因警告而打扰您。你甚至可以创建一个自定义的 VS 插件,为这个自定义扩展提供语法高亮和智能感知。