Custom viewing location in ASP.NET MVC
ASP.NET MVC defaults to search for views in a specific
folder hierarchical structure. However, in some cases, for example, based on naming space tissue views, a custom view search location needs to be customized.
Views
Problem description
Consider the following project structure:
When accessing the controller in the "DEMO" naming space (for example,
), the MVC defaults to search for views in the <code>- Controllers
- Demo
- DemoArea1Controller
- DemoArea2Controller
- Views
- Demo
- DemoArea1
- Index.aspx
- DemoArea2
- Index.aspx</code>
Copy after login
subfolder. However, there will be an error "I can't find the view 'index' or its mother version."
DemoArea1Controller
Solution /Views/DemoArea1
In order to customize the viewing location, it is recommended to expand Class:
Here, you define an array containing a custom view search position. WebFormViewEngine
Register <code class="language-csharp">public class CustomViewEngine : WebFormViewEngine
{
public CustomViewEngine()
{
var viewLocations = new[] {
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx",
"~/AnotherPath/Views/{0}.ascx"
//等等
};
this.PartialViewLocationFormats = viewLocations;
this.ViewLocationFormats = viewLocations;
}
}</code>
Copy after login
After creating a custom view engine, it must be registered in the method of :
By clearing the default view engine and adding a custom engine, you can effectively specify that the controller should search for view views at the specified position. Global.asax.cs
Application_Start
This Revied Output Maintains The Original Image and rewords the text to Achieve a Similar Meaning Direct REPLICATION. in unchanged.
The above is the detailed content of How Can I Customize View Search Locations in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!