简体   繁体   中英

How to use multiple view engines in ASP.NET MVC application

I'd like to use two view engines in my asp.net mvc web application. The first one is the Brail view engine from MVCContrib project, and another one is my custom view engine for generating javascript code. However, I can't configure the application to use both engines. I'd like to use view file extension to discern which engine should handle the request - .brail for Brail engine - .json - for my custom engine

When I add two engines to the ViewEngines collection in global.asax.cs, the system is always looking for *.brail views and throws exception if it doesn't find one. It never looks for my *.json views... Is it a problem with the ASP.Net mvc framework, with Brail view engine or with my code? How should it be handled?

Like this.

EDIT: If your custom ViewEngine isn't handling *.json, you might well have a bug in your view engine. The easiest way to deal with this is to subtype VirtualPathProviderViewEngine and set the MasterLocationFormats, ViewLocationFormats, and PartialViewLocationFormats in the constructor. See the source code for WebFormViewEngine for a very simple example. This way the framework code does all the lifting for you.

I believe you can change your view engine in the controller. So I would extend your controller to select a view engine by extension.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM