Click here to Skip to main content
15,887,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
      }


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 ?
Posted
Updated 10-Oct-17 6:04am

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.
 
Share this answer
 
Comments
Abrar Kazi 9-Oct-17 23:57pm    
I am using Razor Engine still it fetches aspx first.
By default, it looks like the "web forms" view engine is registered before the Razor view engine:
aspnetwebstack/ViewEngines.cs[^]
C#
private static readonly ViewEngineCollection _engines = new ViewEngineCollection
{
    new WebFormViewEngine(),
    new RazorViewEngine(),
};

If you don't need aspx views, you can remove the WebFormViewEngine in the Application_Start event:
C#
var webFormEngine = ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault();
if (webFormEngine != null) ViewEngines.Engines.Remove(webFormEngine);

If you want both view engines, but want Razor views to take priority, you can change the order:
C#
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
ViewEngines.Engines.Add(new WebFormViewEngine());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900