C# 如何让MVC在嵌套文件夹中查找视图

C# 如何让MVC在嵌套文件夹中查找视图,c#,asp.net-mvc-3,razor,C#,Asp.net Mvc 3,Razor,我对MVC和Razor的了解非常基础,所以我希望这是一个相当简单的东西。基本上,我的控制器与平常一样,但我的视图文件夹具有嵌套结构。例如,而不是: Views -> Index.cshtml 就像 Views -> BrandName -> Index.cshtml 我创建了一个自定义帮助器来解决这个问题,但我不确定它如何处理查询字符串URL?以下为控制器示例: private DataService ds = new DataService(); //

我对MVC和Razor的了解非常基础,所以我希望这是一个相当简单的东西。基本上,我的
控制器
与平常一样,但我的
视图
文件夹具有嵌套结构。例如,而不是:

Views -> Index.cshtml
就像

Views -> BrandName -> Index.cshtml
我创建了一个自定义帮助器来解决这个问题,但我不确定它如何处理查询字符串URL?以下为控制器示例:

    private DataService ds = new DataService();

    //
    // GET: /Collections/

    public ActionResult Index()
    {
        return View();
    }


    //
    // GET: /Collections/Collection?id=1
    public ActionResult Collection(int id)
    {
        var collectionModel = ds.GetCollection(id);
        return View(collectionModel);
    }
但是我如何获得
ActionResult集合
来查看:

Views -> Brand2 -> Collection.cshtml
以下是我使用的变通方法:

public static string ResolvePath(string pageName)
    {
        string path = String.Empty;
        //AppSetting Key=Brand
        string brand = ConfigurationManager.AppSettings["Brand"];

        if (String.IsNullOrWhiteSpace(brand))
            path = "~/Views/Shared/Error.cshtml"; //Key [Brand] was not specified
        else
            path = String.Format("~/Views/{0}/{1}", brand, pageName);

        return path;
    }
使用以下命令

public ActionResult Collection(int id)
{
    var collectionModel = ds.GetCollection(id);
    return View("/Brand2/Collection", collectionModel);
}
上面的代码将搜索以下视图

~/Views/Brand2/Collection.aspx
~/Views/Brand2/Collection.ascx
~/Views/Shared/Brand2/Collection.aspx
~/Views/Shared/Brand2/Collection.ascx
~/Views/Brand2/Collection.cshtml
~/Views/Brand2/Collection.vbhtml
~/Views/Shared/Brand2/Collection.cshtml
~/Views/Shared/Brand2/Collection.vbhtml
或者更直接一点

public ActionResult Collection(int id)
    {
        var collectionModel = ds.GetCollection(id);
        return View("~/Brand2/Collection.cshtml", collectionModel);
    }

现在,我想第一个警告你,你永远,永远,永远不要使用这个答案。遵循MVC应用程序中固有的约定是有充分理由的。将文件放在已知的位置可以让每个人都更容易理解您的应用程序。

您可以在应用程序启动时添加视图引擎应该检查的路径,这样做

           RazorViewEngine razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>()
          .FirstOrDefault();

            List<string> viewLocationFormats = new List<string>();
            newPartialViewFormats.Add("~/Views/Shared/MyDirectory/{0}.cshtml");
            //add any other directories you want to check as well

            razorEngine.ViewLocationFormats =
                razorEngine.ViewLocationFormats.Union(viewLocationFormats).ToArray();
RazorViewEngine razorEngine=ViewEngines.Engines.OfType()
.FirstOrDefault();
List viewLocationFormats=新建列表();
添加(“~/Views/Shared/MyDirectory/{0}.cshtml”);
//添加您想要检查的任何其他目录
razorEngine.ViewLocationFormats=
razorEngine.ViewLocationFormats.Union(ViewLocationFormats.ToArray();

对于局部视图,您可以执行类似的操作,但可以使用
razorEngine.PartialViewLocationFormats
属性进行操作。

不清楚。你已经有了一个解决方案(大部分都有效),但还是发布了一个非常简单的控制器?@Henkholtman我认为没有太多的意义继续进行这个解决方案,因为可能有一种方法可以做到这一点naturally@HenkHolterman不知道这个问题怎么不清楚比如,它是
Views/Controller/Brand1/Index.cshtml
还是
Views/Brand1/Controller/Index.cshtml
?控制器最好在那里,我认为顺序很重要。@Henkholtman完全如问题中所述……Views/Brand1/Index.cshtmless比理想值低,但为了工作安全+1。