gpt4 book ai didi

asp.net-mvc - 我可以在 ASP.NET MVC 中为 "search for views"指定一个自定义位置吗?

转载 作者:行者123 更新时间:2023-12-01 23:10:25 25 4
gpt4 key购买 nike

我的 mvc 项目具有以下布局:

  • / Controller
  • /演示
  • /Demo/DemoArea1Controller
  • /Demo/DemoArea2Controller
  • 等等...
  • /浏览次数
  • /演示
  • /Demo/DemoArea1/Index.aspx
  • /Demo/DemoArea2/Index.aspx

  • 但是,当我为 DemoArea1Controller 准备了这个时:
    public class DemoArea1Controller : Controller
    {
    public ActionResult Index()
    {
    return View();
    }
    }

    我收到“无法找到 View ‘索引’或其主文件”错误,以及通常的搜索位置。

    如何在“Demo” View 子文件夹中的“Demo”命名空间搜索中指定 Controller ?

    最佳答案

    您可以轻松扩展 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 方法来注册 View 引擎
    protected void Application_Start()
    {
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
    }

    关于asp.net-mvc - 我可以在 ASP.NET MVC 中为 "search for views"指定一个自定义位置吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/632964/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com