I have an unforeseen problem regarding a sort of Multi Tenancy implementation in ASP.NET MVC 3.
Say I have 2 websites: example.com and example.fr. They are both served by the same MVC Website in IIS.
Then I have a custom VirtualPathProvider that, based on the domain, serves Views from different locations. The Controller is always the same, only the Views are fetched from different locations.
This all works well. The problem comes with ASP.NET View compilation. Suppose both domains have a View with the same name and path (MVC views path for clarity):
example.com/Views/MyController/Index.cshtml
example.fr/Views/MyController/Index.cshtml
This should work well. But the ASP.NET BuildManager (which compiles the Razor code to assemblies), caches the build based only on the Virtual Path.
So this means that when I first render a View while visiting example.com I get the correct view. But if I then try to render the View in the context of example.fr, ASP.NET considers that the view has not been modified (which is true as the Virtual Path is the same) and it will execute the view from cache, hence rendering the incorrect View.
A way to solve it is to maybe have the Views being compiled in different namespaces, based on the domain.
So far I got to MvcWebRazorHostFactory, override CreateHost method to return a RazorEngineHost with the correct namespace. Not sure if it will work because I don’t think I have all the needed information at that point (HttpContext is one of them)
Anyone has any ideas? Am I missing something obvious here?
Thanks
Ok, It turns out to be simple to solve.
All I had to do was override
GetCacheKeyin myVirtualPathProviderand return a key string that takes into account the hostname.In my case I’m simply concatenating the host and the virtual path and return the hash code of the resulting string.