Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour when loading razor view from custom VirtualPathProvider (ASHX Source)

I get some strange behaviour when I load data via my custom VirtualPathProvider. It might be important to mention, that I am trying to use this view as a layout.

public class MyVirtualPathProvider : VirtualPathProvider
{
    public MyVirtualPathProvider()
        : base()
    {

    }

    public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart) 
    {
        if ((virtualPath.StartsWith("/Path/") ||
            virtualPath.StartsWith("~/Path/")) && virtualPath.EndsWith(".cshtml"))
        {
            String name = virtualPath.Replace("/Path/", "").Replace(".cshtml", "");
            Uri uri = new Uri("http://www.example.com/Handler.ashx?path=" + name);
            return new WebCacheDependency(uri.ToString());
        }

        return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
    }

    public override bool FileExists(string virtualPath)
    {
        if ((virtualPath.StartsWith("/Path/") || 
            virtualPath.StartsWith("~/Path/")) && virtualPath.EndsWith(".cshtml"))
            return true;

        return base.FileExists(virtualPath);
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        if (virtualPath.StartsWith("/Path/") || virtualPath.StartsWith("~/Path/"))
            return new TemplateVirtualFile(virtualPath);

        return base.GetFile(virtualPath);
    }
}

I also have implemented a custom (dummy) CacheDependency

public class WebCacheDependency : CacheDependency
{
    public WebCacheDependency(String url)
    {
        this.SetUtcLastModified(DateTime.UtcNow);
    }
}

Now there are two things that don't work. First, all loaded views are being cached and secondly, code inside the file (@Html.ActionLink ... etc.) does not work, It just gives an error "assembly missing".

Has anyone an idea how to remedy those two problems?

There's already a solution of the second problem (link) however I really don't get it how this problem can be solved inside the FileExists method.

Thank you!

Update: An image of the error message enter image description here

like image 907
Dänu Avatar asked Feb 15 '26 00:02

Dänu


1 Answers

Do you have your web.config properly setup for Razor? If you don't specify the pageBaseType of WebViewPage you won't have access to the ViewBag as its not in the default base type, WebPageBase.

Your web.config should look something like this:

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    [...]
  </pages>
</system.web.webPages.razor>

Note the pageBaseType attribute. More info here: http://msdn.microsoft.com/en-us/library/system.web.webpages.razor.configuration.razorpagessection.pagebasetype(v=vs.99).aspx

By default, the default value for PageBaseType is System.Web.WebPages.WebPage.

like image 196
Paul Tyng Avatar answered Feb 21 '26 16:02

Paul Tyng



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!