超过混合MVC3项目ASPX剃刀意见优先 [英] Priority of razor views over the aspx in mixed MVC3 project

查看:162
本文介绍了超过混合MVC3项目ASPX剃刀意见优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经转换旧MVC2项目MVC3。现在我已经用剃刀.cshtml一起的.aspx意见。

I've converted old MVC2 project to MVC3. Now I have .aspx views along with razor .cshtml.

比方说,我有一个控制器(的HomeController 首页动作和相关联的视图〜\\查看\\首页\\ Index.aspx的),并在同一时间,我还是有完全不同的〜\\查看\\共享\\ Index.aspx的

Let's say I have a view associated with controller (HomeController, Index action and ~\Views\Home\Index.aspx) and at the same time I still have absolutely different ~\Views\Shared\Index.aspx.

通常,当首页行动电话查看()它呈现〜\\查看\\首页\\ Index.aspx的。但是,如果我转换成视图查看剃须刀,同样的动作,而不是渲染〜\\查看\\首页\\ Index.cshtml 通话
〜\\查看\\共享\\ Index.aspx的

Normally when Index Action calls View() it renders ~\Views\Home\Index.aspx. But if I convert the view into razor view, the same action instead of rendering ~\Views\Home\Index.cshtml calls ~\Views\Shared\Index.aspx.

所以我想MVC优​​先为.aspx页面,而到.cshtml。也许我需要更改web.config文件中的东西,因为现在我要明确告诉它哪个视图来获得:

So I guess MVC gives priority to .aspx pages rather to .cshtml. Maybe I need to change something in web.config files, because now I have to explicitly tell it which view to get:

View("~\Views\Home\Index.cshtml")

即使我把扩展视图(〜\\查看\\首页\\指数)它仍然会调用共享的.aspx观点,虽然我已经在正确的道路。奇怪,不是吗?

Even if I drop the extension View("~\Views\Home\Index") it will still call the shared .aspx view, although I have the right path. Strange isn't it?

推荐答案

您所遇到的问题是MVC观点的默认优先级。下面是默认的:

The problem you are having is the default priority of MVC views. Here is the default:

~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 

正如你所看到的,这有利于所有的ASPX / ASCX文件,甚至在共享目录中,之前类似剃须刀的看法。

As you can see, it favors all aspx/ascx files, even in the shared directory, before similar razor views.

解决方案之一是获取MVC剃刀青睐.cshtml过的.aspx 然而,这种解决方案并不重新关联的ASPX / ASCX文件。所以,你需要这样的:

One solution is to Get MVC Razor favor .cshtml over .aspx, however this solution does NOT reassociated the aspx/ascx files. So you'd need something like:

protected void Application_Start() 
{ 
  ViewEngines.Engines.Clear(); 
  ViewEngines.Engines.Add(new RazorViewEngine()); 
  ViewEngines.Engines.Add(new WebFormViewEngine()); 
} 

这篇关于超过混合MVC3项目ASPX剃刀意见优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆