控制器首先调用aspx或cshtml是什么? [英] What does the controller calls first aspx or cshtml ?
问题描述
我有以下
Controller / HomeController
Views / Home.cshtml和Home.aspx在同一个文件夹中
现在当以下代码执行时;
I have the following
Controller/HomeController
Views/Home.cshtml and Home.aspx in same folder
Now when this below code executes;
public ActionResult Index()
{
return View();
}
控制器首先获取Home.aspx而不是Home.cshtml。
任何理由?为什么选择aspx而不是cshtml?
我尝试了什么:
我尝试了代码,我明白它首先获取aspx但我想知道为什么?
Controller fetches Home.aspx first not Home.cshtml.
Any reason ? Why does it select aspx over cshtml ?
What I have tried:
I tried the code and i understood that it first fetches aspx but i want to know WHY ?
推荐答案
这取决于视图引擎。
如果您使用的是Razor视图引擎,它将选择.sshml超过.aspx。
如果您使用的是网络表单,那么它会选择。 .xshtml上的aspx(或部分视图的.ascx)。
That depends on the view engine.
If you are using the Razor view engine, it will choose .cshml over .aspx.
If you're using web forms, then it will choose .aspx (or .ascx for partial views) over .cshtml.
默认情况下,在Razor视图引擎之前注册web forms视图引擎:
aspnetwebstack / ViewEngines.cs [ ^ ]
By default, it looks like the "web forms" view engine is registered before the Razor view engine:
aspnetwebstack/ViewEngines.cs[^]
private static readonly ViewEngineCollection _engines = new ViewEngineCollection
{
new WebFormViewEngine(),
new RazorViewEngine(),
};
如果您不需要 aspx
视图,则可以删除 WebFormViewEngine
在 Application_Start
事件中:
If you don't need aspx
views, you can remove the WebFormViewEngine
in the Application_Start
event:
var webFormEngine = ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault();
if (webFormEngine != null) ViewEngines.Engines.Remove(webFormEngine);
如果您想要两个视图引擎,但希望Razor视图优先,您可以更改顺序:
If you want both view engines, but want Razor views to take priority, you can change the order:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
ViewEngines.Engines.Add(new WebFormViewEngine());
这篇关于控制器首先调用aspx或cshtml是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!