本文介绍了是否有可能在MVC多个共享文件夹,并在其他共享文件夹执行的RenderPartial到的局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC5,我知道你可以在查看一个共享文件夹然后使用的RenderPartial 来在局部视图渲染。

In MVC5, I know you can have a Shared folder under Views and then use the RenderPartial to render in partial views.

有永远只能有一个共享文件夹整个网站?还是有可能有多个共享的地方?

Is there only ever one Shared folder for the whole website?, or is it possible to have multiple 'Shared' areas?

比如说,我有以下结构在我的网站: -

For instance, I have the following structure on my website:-

\\查看

\\浏览\\共享

\\控制器

\\型号

\\身份

\\标识\\查看

\\标识\\控制器

\\标识\\型号

我想知道,如果它也有可能为身份文件夹有自己的共享文件夹,以及,这也的RenderPartial将工作?

I was wondering if it would also be possible for the Identity folder to have its own Shared folder as well, that RenderPartial would also work for?

如果这是可能的,我可以呈现 PartialView 从这个其他共享文件夹?我试过这个,但没有成功 - 即使我使用波浪号〜办法直接引用视图,但它似乎不喜欢跑步抛出例外。但是,如果我在 \\浏览\\共享文件夹放在 PartialView 然后一切正常。

If this is possible, can I render a PartialView from this other Shared folder? I had tried this but wasn't successful - even if I directly reference the View using the tilde ~ approach, but it doesn't seem to like running throwing an exception. However if I put the PartialView in my \Views\Shared folder then everything works.

推荐答案

您可以通过在下面添加code包括在视图引擎的目录的Global.asax 的Application_Start()事件:

You can include your directories in the ViewEngine by adding following code in Global.asax in Application_Start() event:

RazorViewEngine razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
if (razorEngine != null)
{
  string[] newPartialViewFormats = new[] 
          { 
            "~/Indentity/Views/{1}/{0}.cshtml",
            "~/Identity/Views/Shared/{0}.cshtml"
          };

  razorEngine.PartialViewLocationFormats = razorEngine.PartialViewLocationFormats.Union(newPartialViewFormats).ToArray();
 }

现在的ViewEngine会发现在视图中的查看/共享以及在身份/查看/共享

Now ViewEngine will find the view in View/Shared and also in Identity/Views/Shared

您可以参考,也请参阅本SO帖子

You can refer this link and also refer this SO Post.

这篇关于是否有可能在MVC多个共享文件夹,并在其他共享文件夹执行的RenderPartial到的局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 08:18