使用区域时如何在MVC3中指定到共享视图的路由 [英] How to specify route to shared views in MVC3 when using Areas

查看:98
本文介绍了使用区域时如何在MVC3中指定到共享视图的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Areas的MVC3/Razor应用程序,因此我的视图位于以下位置:

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

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

/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?

我在想这样的事情:

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

谢谢!

推荐答案

我通常从VirtualPathProvider继承一个类

I usually descend a class from VirtualPathProvider

在这里查看更多信息:

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

然后在启动时注册它:

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

    }

这篇关于使用区域时如何在MVC3中指定到共享视图的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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