Home > Backend Development > C++ > How Can I Customize View Search Locations in ASP.NET MVC?

How Can I Customize View Search Locations in ASP.NET MVC?

Mary-Kate Olsen
Release: 2025-01-26 01:01:10
Original
626 people have browsed it

How Can I Customize View Search Locations in ASP.NET MVC?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template