使用区域时如何在 MVC3 中指定共享视图的路由

发布于 2024-11-15 19:02:58 字数 430 浏览 2 评论 0原文

我有一个使用区域的 MVC3/Razor 应用程序,这样我的视图位于以下位置:

/Areas/Areaname/Views/ControllerName/view.cshtml

现在,对于任何共享部分,我必须将它们放在这里:

/Views/Shared/_sharedview.cshtml

我更愿意这里有我的共享视图:

/Areas/Shared/Views/_sharedvew.cshtml

有没有办法告诉视图引擎查看共享视图默认位置之外的其他位置?

我在想这样的事情:

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

谢谢!

I have an MVC3/Razor app using Areas such that my views are in the following location:

/Areas/Areaname/Views/ControllerName/view.cshtml

Right now, for any shared partials, I have to put them here:

/Views/Shared/_sharedview.cshtml

I would prefer to have my shared views here:

/Areas/Shared/Views/_sharedvew.cshtml

Is there a way to tell the view engine to look somewhere other than the default location for shared views?

I am thinking something like this:

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

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昵称有卵用 2024-11-22 19:02:58

我通常从 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;
        }

    }

I usually descend a class from VirtualPathProvider

Have a look here for more:

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

then register it in the startup:

HostingEnvironment.RegisterVirtualPathProvider(new EmbeddedViewPathProvider());

You do not have to deal with paths that are outside of your scope as you can pass the function on the the base definition as I do here in this embedded view path provider:

  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;
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文