且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以指定自定义位置"搜索意见"在ASP.NET MVC?

更新时间:2023-02-17 08:37:26

您可以轻松地扩展WebFormViewEngine指定所有要在看的位置:

You can easily extend the WebFormViewEngine to specify all the locations you want to look in:

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"
            // etc
        };

        this.PartialViewLocationFormats = viewLocations;
        this.ViewLocationFormats = viewLocations;
    }
}

请确保你记得在你的Global.asax.cs修改Application_Start方法中注册视图引擎

Make sure you remember to register the view engine by modifying the Application_Start method in your Global.asax.cs

protected void Application_Start()
{
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
}