如何在ASP.NET MVC应用程序中使用多个视图引擎

时间:2022-11-01 04:12:09

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

我想在我的asp.net mvc web应用程序中使用两个视图引擎。第一个是来自MVCContrib项目的Brail视图引擎,另一个是用于生成javascript代码的自定义视图引擎。但是,我无法将应用程序配置为使用两个引擎。我想使用视图文件扩展来识别哪个引擎应该处理请求 - .brail for Brail引擎 - .json - 用于我的自定义引擎

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?

当我在global.asax.cs中向ViewEngines集合添加两个引擎时,系统总是在查找* .brail视图,如果找不到,则抛出异常。它从不寻找我的* .json视图......它是ASP.Net mvc框架的问题,Brail视图引擎还是我的代码?该如何处理?

2 个解决方案

#1


3  

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.

编辑:如果你的自定义ViewEngine没有处理* .json,你的视图引擎可能会有一个错误。处理此问题的最简单方法是子类型VirtualPathProviderViewEngine并在构造函数中设置MasterLocationFormats,ViewLocationFormats和PartialViewLocationFormats。有关一个非常简单的示例,请参阅WebFormViewEngine的源代码。这样,框架代码可以为您完成所有工作。

#2


2  

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

我相信你可以在控制器中更改你的视图引擎。所以我会扩展你的控制器以扩展选择一个视图引擎。

#1


3  

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.

编辑:如果你的自定义ViewEngine没有处理* .json,你的视图引擎可能会有一个错误。处理此问题的最简单方法是子类型VirtualPathProviderViewEngine并在构造函数中设置MasterLocationFormats,ViewLocationFormats和PartialViewLocationFormats。有关一个非常简单的示例,请参阅WebFormViewEngine的源代码。这样,框架代码可以为您完成所有工作。

#2


2  

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

我相信你可以在控制器中更改你的视图引擎。所以我会扩展你的控制器以扩展选择一个视图引擎。