-1

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.

Dhanuka777
  • 8,331
  • 7
  • 70
  • 126
  • Please have a look https://stackoverflow.com/questions/9300410/where-and-how-is-the-viewstart-cshtml-layout-file-linked – Antipod Feb 24 '18 at 15:14
  • @Dhanuka777 Could you please try the approach given in my answer below and let me know if it works? Thanks in advance... – Murat Yıldız Apr 22 '18 at 09:27

1 Answers1

0

You can try to define layout page in _ViewStart.cshtml:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

If you use different layout pages for several Views, you can also define corresponding layout pages for every View as shown below:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
Murat Yıldız
  • 11,299
  • 6
  • 63
  • 63