2011-06-16 82 views
0

我使用领域,我的观点是在以下位置的MVC3 /剃刀应用程序时,指定要在MVC3共享视图航线如何使用领域

现在,对于任何共享的谐音,我必须把他们在这里:

/Views/Shared/_sharedview.cshtml 

我宁愿在这里有我的共享访问量:

/Areas/Shared/Views/_sharedvew.cshtml 

有没有办法让视图引擎看起来不是共享视图的默认位置?

我想是这样的:

routes.MapRoute("Shared", "Areas/Shared/Views/{id}"); 

谢谢!

回答

0

我通常是从的VirtualPathProvider

下降一类

看看这里为更多:

http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/

然后在启动时进行注册:

HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewPathProvider()); 

您不必处理超出范围的路径,因为您可以像在此嵌入视图中那样在基本定义上传递函数路径提供:

public class EmbeddedViewPathProvider: VirtualPathProvider 
    { 
     static EmbeddedViewPathProvider() 
     { 
      ResourcePaths = new Dictionary<string, ViewResource>(); 
      foreach (var resource in SettingsManager.Get<CoreSettings>().Assemblies.Select(assembly => new ViewResource 
                              { 
                               VirtualPath = "/views/embedded/" + assembly.ToLower(), AssemblyName = assembly 
                              })) 
      { 
       AddResource(resource); 
      } 
     } 

     public static void AddResource(ViewResource assemblyResource) 
     { 
      ResourcePaths.Add(assemblyResource.VirtualPath, assemblyResource); 
     } 
     private static Dictionary<string, ViewResource> ResourcePaths { get; set; } 

     public bool IsAppResourcePath(string virtualPath) 
     { 

      var checkPath = VirtualPathUtility.ToAppRelative(virtualPath).ToLower(); 
      return ResourcePaths.Any(resourcePath => checkPath.Contains(resourcePath.Key) && ResourceExists(resourcePath.Value, checkPath)); 
     } 

     private static bool ResourceExists(ViewResource assemblyResource, string path) 
     { 
      var name = assemblyResource.GetFullyQualifiedTypeFromPath(path); 
      return Assembly.Load(assemblyResource.AssemblyName).GetManifestResourceNames().Any(s => s.ToLower().Equals(name)); 
     } 


     public ViewResource GetResource(string virtualPath) 
     { 
      var checkPath = VirtualPathUtility.ToAppRelative(virtualPath).ToLower(); 
      return (from resourcePath in ResourcePaths where checkPath.Contains(resourcePath.Key) select resourcePath.Value).FirstOrDefault(); 
     } 

     public override bool FileExists(string virtualPath) 
     { 
      var exists = base.FileExists(virtualPath); 
      return exists || IsAppResourcePath(virtualPath); 
     } 

     public override VirtualFile GetFile(string virtualPath) 
     { 
      if (IsAppResourcePath(virtualPath) && !base.FileExists(virtualPath)) 
      { 
       var resource = GetResource(virtualPath); 
       return new ViewResourceVirtualFile(virtualPath, resource); 
      } 
      return base.GetFile(virtualPath); 

     } 

     public override CacheDependency 
      GetCacheDependency(string virtualPath, 
           IEnumerable virtualPathDependencies, 
           DateTime utcStart) 
     { 
      if (IsAppResourcePath(virtualPath)) 
      { 
       return null; 
      } 
      var dependencies = virtualPathDependencies.OfType<string>().Where(s => !s.ToLower().Contains("/views/embedded")).ToArray(); 
      return base.GetCacheDependency(virtualPath, dependencies, utcStart); 

     } 

     public override string GetCacheKey(string virtualPath) 
     { 
      return null; 
     } 

    } 
+0

这似乎表明,我要创建一个从的VirtualPathProvider继承的自定义视图引擎,但我用的剃须刀,所以我有点不清楚如何做到这一点的工作。您提供的链接似乎解决了与您描述的情况稍有不同的情况。你能详细解释一下吗?谢谢 – sydneyos 2011-06-17 21:54:37

+0

所以,我将此标记为答案,虽然这有点误导(即链接)。似乎答案是创建一个从VirtualPathProvider继承的类,然后按照说明在启动时注册它。我没有这样做,因为它基本上拦截了每个路径请求,你必须重写和实现你自己的逻辑。这只是为了方便我在一个地方看到我的所有观点而开销太多。希望有一种更简单的方法来配置默认路径提供程序。唉。 – sydneyos 2011-06-28 01:33:32

+0

已经添加了代码来演示如何以嵌入式视图为例,但是如果在路径中找不到,则可以传递给基类。 – Richard 2011-06-28 08:12:48