Search code examples
visual-studio-2010asp.net-mvc-3vxml

How do I use Visual Studio 2010 MVC 3 for a non-html based project?


I'd like to use the Visual Studio 2010 MVC 3 framework for a web project that hosts VXML and CCXML rather than HTML for telephony based applications.

MVC 3 seems ideally suited to this kind of application, and I think it's a great alternative to the more widely used jsp/Tomcat based applications that are generally used for VXML/CCXML. But there are a couple of annoyances:

  1. The project tries to validate my .cshtml pages as HTML (understandably), or whatever target validation I set in Options>Text Editor>HTML>Validation>Target, but it forces me to have validation. I can't seem to find a way to switch it off altogether. Is there a way to switch it off and prevent hundreds of warnings? or better still, write my own custom validation for CCXML/VXML?
  2. Is there a way to automatically set the content-type to "text/vxml" for all views without having to write Response.ContentType = "text/vxml"; in every Action Method?

Solution

  • You could write a custom view engine based on the Razor view engine and register a custom extension for your views:

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

    and in Application_Start declare the custom extension:

    RazorCodeLanguage.Languages.Add("vxml", new CSharpRazorCodeLanguage());
    WebPageHttpHandler.RegisterExtension("vxml");
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new VXMLViewEngine());
    

    and the custom build provider in web.config that will recognize the .vxml extension:

    <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>
    

    Now you could use .vxml as extension for the views. Since Visual Studio doesn't recognize the .vxml extension, when you open a file with it, it won't bother you with warnings. And you could even create a custom VS plugin that could provide syntax highlighting and Intellisense for this custom extension.