我是否可以在ASP.NETMVC中指定“搜索视图”的自定义位置?

我是否可以在ASP.NETMVC中指定“搜索视图”的自定义位置?

我的MVC项目有以下布局:

  • /控制器
    • /演示
    • /Demo/DemoArea1控制器
    • /Demo/DemoArea 2控制器
    • 等等.。
  • /意见
    • /演示
    • /Demo/DemoArea 1/Index.aspx
    • /Demo/DemoArea 2/Index.aspx

但是,当我有了这个DemoArea1Controller:

public class DemoArea1Controller : Controller{
    public ActionResult Index()
    {
        return View();
    }}

我得到“视图‘索引’或它的主错误找不到”错误,与通常的搜索位置。

如何指定“Demo”视图子文件夹中“Demo”命名空间搜索中的控制器?


慕斯709654
浏览 604回答 3
3回答

斯蒂芬大帝

您可以轻松地扩展WebFormViewEngine以指定要查看的所有位置: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方法来注册视图引擎。protected void Application_Start(){     ViewEngines.Engines.Clear();     ViewEngines.Engines.Add(new CustomViewEngine());}
打开App,查看更多内容
随时随地看视频慕课网APP