How to specify route to shared views in MVC3 when using Areas?

I usually descend a class from VirtualPathProvider.

Up vote 0 down vote favorite share g+ share fb share tw.

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!

Asp.net-mvc-3 asp.net-mvc-3-areas link|improve this question asked Jun 16 '11 at 16:10sydneyos361211 91% accept rate.

I usually descend a class from VirtualPathProvider Have a look here for more: coderjournal.com/2009/05/creating-your-f... 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(); foreach (var resource in SettingsManager.Get().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 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(). Where(s =>!s.ToLower().

Contains("/views/embedded")).ToArray(); return base. GetCacheDependency(virtualPath, dependencies, utcStart); } public override string GetCacheKey(string virtualPath) { return null; } }.

This seems to suggest that I have to create a custom ViewEngine that inherits from VirtualPathProvider, but I am using Razor, so I'm a little unclear how this would work. The link you provided seems to address a somewhat different scenario than what you describe. Could you elaborate just a bit?

Thanks – sydneyos Jun 17 '11 at 21:54 So, I'm marking this as the answer, though it's a little misleading (the link, that is). Seems the answer is to create a class that inherits from VirtualPathProvider and then register it on startup as described. I didn't do it, because basically it intercepts every path request and you have to override and implement your own logic.

It just seemed like too much overhead just for the convenience of having all my views in one place. Was hoping there was a simpler way to configure the default path provider. Alas.

– sydneyos Jun 28 '11 at 1:33 Have added code to show how to do it with embedded views as an example but you can pass to the base class when not found in your paths. – Richard Jun 28 '11 at 8:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions