Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html helper does not use custom VirtualPathProvider

I have set up a VirtualPathProvider and it is working fine for direct url calls like http://.../home/index in the address bar.

public class HomeController
{   
    public ActionResult Index()
    {
        // This triggers MyVirtualPathProvider functionallity when called via
        // the browsers address bar or anchor tag click for that matter.
        // But it does not trigger MyVirtualPathProvider for 'in-view' calls like
        // @{ Html.RenderAction("index", "home"); }
        return View();
    }
}

public class MyVirtualPathProvider : VirtualPathProvider
{

    public override System.Web.Hosting.VirtualFile GetFile(string virtualPath)
    {
        // This method gets hit after the Controller call for return View(...);
        if (MyCondition)
            return MyVirtualFileHandler.Get(virtualPath);
        return base.GetFile(virtualPath);
    }

    public override bool FileExists(string virtualPath)
    {
        // This method gets hit after the Controller call for return View(...);
        if (MyCondition)
            return true;
        return base.FileExists(virtualPath);
    }

}

However, I would like this to work for the Html helper too, but right now it is ignoring the VirtualPathProvider for the html helper calls in the view:

@{
     Html.RenderAction("index", "home");
}

Is there a way to solve this problem?

In addtion I have an override for the WebViewPage so I would be able to override the initialization for helpers, but I haven't got a clue with what or how.

Edit:

I have tried this at two computers and, oddly enough, it works on another computer. So the question would actually become:

Why does the VirtualPathProvider works on one and fails for 50% on another computer? But then this question would then become somewhat to vague, speculative even. Nonetheless I am not happy with this but it seems I would have to reinstall some things. :(

like image 310
Silvermind Avatar asked Dec 16 '13 10:12

Silvermind


1 Answers

User LostInComputer was kind enough to hand over a sample project which worked on my notebook and I was confused about what would be the difference.

Normally one would expect indeed that Html helpers would just work for the VirtualPathProvider, and now I know it should.

The actual problem lies within the installation of the particular pc where I encountered the problem on and after a reinstall everything worked just fine. So it is not really a sophisticated solution, but since there has been little attention to this question I am at least giving it my own answer, because this could be useful stuff to someone else some day, how dull it might be. :)

When you would surely expect something to work, you could always try to run it on a different machine (if you have one available of course), because in the end all that could be wrong might just be a corrupt installation. :(

like image 192
Silvermind Avatar answered Nov 14 '22 03:11

Silvermind