Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I change the search order for the physical views of an ASP.Net MVC 3 application

I noticed Asp.net MVC 3 searches for .aspx files before .cshtml files. Can I change this search order? And how to do this?

Background information

When debugging I got the following exception:

The view 'Reset' or its master was not found. The following locations were searched:
~/Views/Demo/Reset.aspx
~/Views/Demo/Reset.ascx
~/Views/Shared/Reset.aspx
~/Views/Shared/Reset.ascx
~/Views/Demo/Reset.cshtml
~/Views/Demo/Reset.vbhtml
~/Views/Shared/Reset.cshtml
~/Views/Shared/Reset.vbhtml

I conclude from this, that the old .aspx views are searched for first.

Since I converted my site to MVC3, and all views to Razor, I would like the .cshtml files to be searched first. I think this would be better for performance.

like image 264
GvS Avatar asked Nov 16 '10 20:11

GvS


People also ask

How sorting is implemented in MVC?

Add sorting functionality to the Index method The query string value is provided by ASP.NET MVC as a parameter to the action method. The parameter is a string that's either "Name" or "Date", optionally followed by an underscore and the string "desc" to specify descending order. The default sort order is ascending.

Can one action have multiple views?

No you cant have multiple views returned from single action. In case you want in that way then, you can do it like this, Parent View Partial View inside Parent View.

How do you use same view for add and edit in MVC?

Use the same View Model aka CreateUpdateViewModel and discarding extra UpdateBindingModel properties in the view but still posting the corresponding model on POST. Having your binding models as properties and select one or the other in the specific view. (better use @Html.


2 Answers

Yes. Change the order of the existing view engines..

But in a non-debug configuration, file locations are cached, so it will only help on the first lookup. I wouldn't sweat it.

like image 178
Craig Stuntz Avatar answered Oct 02 '22 10:10

Craig Stuntz


Thanks to the answer of Craig Stuntz I found the syntax I was looking for:

I added this to my Application_Start in Global.asax.cs:

    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new RazorViewEngine()); 
    // ViewEngines.Engines.Add(new WebFormViewEngine()); <-- uncomment if needed

This clears the registered ViewEngines, and adds them in the order I want.

like image 23
GvS Avatar answered Oct 02 '22 12:10

GvS