我可以指定自定义位置"搜索意见"在ASP.NET MVC? [英] Can I specify a custom location to "search for views" in ASP.NET MVC?

查看:104
本文介绍了我可以指定自定义位置"搜索意见"在ASP.NET MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下布局我的MVC项目:

I have the following layout for my mvc project:


  • /控制器

    • /演示

    • /演示/ DemoArea1Controller

    • /演示/ DemoArea2Controller

    • 等...


    • /演示

    • /Demo/DemoArea1/Index.aspx

    • /Demo/DemoArea2/Index.aspx

    然而,当我有这样的 DemoArea1Controller

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

    我得到的视图索引或它的主人无法找到错误,与通常的搜索位置。

    I get the "The view 'index' or its master could not be found" error, with the usual search locations.

    我怎么可以指定在演示子文件夹视图演示命名空间搜索控制器?

    How can I specify that controllers in the "Demo" namespace search in the "Demo" view subfolder?

    推荐答案

    您可以轻松地扩展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());
    }
    

    这篇关于我可以指定自定义位置"搜索意见"在ASP.NET MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆