I am trying to load a MVC view from a dll (Asp.Net MVC 5).
Set up I have a MVC web application project and a class library project (Custom.Views) which I have /CustomViews/Views/MyView/CustomView1.cshtml
CustomView1.cshtml which is an embedded resource,
@inherits System.Web.Mvc.WebViewPage
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
Helloooo Custom view in action!!!!!
</div>
In my Custom.Views project, I have a controller called MyView controller,
public ActionResult Shop()
{
return View("~/ClientView/Custom.Views.DLL/Custom.Views.CustomViews.Views.MyView.CustomView1.cshtml");
}
Which returns a virtual view path pointing to the embedded resource. I configured Route config to check this namespace "Custom.Views.CustomViews.." controllers.
I implemented a VirtualPathProvider
,
public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider
{
public AssemblyResourceProvider() { }
private bool IsAppResourcePath(string virtualPath)
{
String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
return checkPath.StartsWith("~/ClientView/", StringComparison.InvariantCultureIgnoreCase);
}
public override bool FileExists(string virtualPath)
{
return (IsAppResourcePath(virtualPath) ||
base.FileExists(virtualPath));
}
public override VirtualFile GetFile(string virtualPath)
{
if (IsAppResourcePath(virtualPath))
return new AssemblyResourceVirtualFile(virtualPath);
else
return base.GetFile(virtualPath);
}
public override CacheDependency GetCacheDependency(string virtualPath,
IEnumerable virtualPathDependencies, DateTime utcStart)
{
if (IsAppResourcePath(virtualPath))
return null;
else
return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}
}
class AssemblyResourceVirtualFile : VirtualFile
{
string path;
public AssemblyResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override System.IO.Stream Open()
{
string[] parts = path.Split('/');
string assemblyName = parts[2];
string resourceName = parts[3];
assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName);
var assembly = Assembly.LoadFile(assemblyName);
if (assembly != null)
{
return assembly.GetManifestResourceStream(resourceName);
}
return null;
}
}
Problem
Question 1. When I type url "httlp://localhost:1234/myview/shop" it hits the virtual path and returns finds the file stream. That part works fine. But soon after that Virtual Path provider gets another request looking for a _ViewStart.cshtml in "~/ClientView/Custom.Views.DLL/_ViewStart.cshtml". Why is this happening? Why it's looking for a _ViewStart.cshtml in that path.