External razor views can't see external models
Asked Answered
G

3

0

I have a problem with external razor views. In my project I have main mvc web assembly and dynamically loaded external class library assemblies(from DB) with their own Controllers, Views and Models. These assemblies are not directly referenced and loaded at runtime.

I was able to make the whole system work by creating a custom controller factory for Controllers, a custom virtual path provider for Views. The views are embedded resources in my external assemblies.

The problem I have is when I create a strongly-typed external View with a model class from an external assembly the view cannot be compiled at runtime, because the assembly is not passed to the razor compiler. So I get the following error:

Compiler Error Message: CS0234: The type or namespace name 'MyPlugin' does not exist in the namespace 'MyNamespace' (are you missing an assembly reference?)

Source Error:

public class
_Page_ExternalViews_MyController_MyAction_cshtml : System.Web.Mvc.WebViewPage<MyNamespace.MyPlugin.Models.MyModel>
{

It works fine when I use a dynamic model, a model class from my main web assembly or from assemblies referenced directly in my web project. I know for sure that external assembly is loaded before the external razor views are compiled, since my controllers work just fine.

I started looking at RazorGenerator project to precompile my external Views, but wasn't able to make any progress (nothing is being generated) and I'm not even sure if I'm looking in the right direction, since my assemblies are loaded at runtime and I have to use my own ViewEngine and ControllerFactory.

Gylys answered 24/7, 2013 at 0:37 Comment(8)
Have you tried either of the options mentioned here: #4953830Socman
@Brent I can't reference my assemblies in web.config, because they are not available before web Application starts. Otherwise yeah, it would solve the problem.Gylys
@Maskim How many external assemblies are you referencing to? Is it just the one, or are there a few?Socman
Could you explain how your Controllers find these models during compilation if they are loaded at runtime? Do you not reference them from your controllers?Arnie
@Arnie controllers, models and views are inside same loaded assemblies, so they are compiled with models.Gylys
@BrentMannering, there's no direct reference, they are loaded at runtime, it can be one or many.Gylys
@Maskim I am still thinking the web.config option may work. When you are defining the namespaces in the config, the assemblies do not need to be referenced in the project, so this does not cause build/compile errors. The assemblies referenced in the config should be "validated" at run-time as the view is being built. Otherwise would you be able to post some code, to show your controller factory etc.Socman
@Brent, it doesn't work for multiple reasons - assembly is not in the app domain at the time when web.config is checked, so it will throw a compilation error. And assemblies are loaded dynamically for a reason, if I was aware of all assemblies being loaded at compile time, I would just reference them directly. The answer below does pretty much the same thing, but at the time when external views are compiled. The only problem is that the razor compiler refuses to see the assembly views are coming with.Gylys
G
0

I was able to precompile views with RazorGenerator Visual Studio extension (not RazorGenerator.Mvc one) in my assemblies.

It basically converts .cshtml razor views into .cs files with WebViewPage classes before assemblies compile. And in my web project I had to implement my own VirtualPathProviderViewEngine similar to this one

Gylys answered 25/7, 2013 at 22:23 Comment(0)
D
1

Try use the using directive in your Views in the dynamic assembly.

@using MyNamespace.MyPlugin.Models;
@using MyNamespace.MyPlugin;

etc

Divulgate answered 24/7, 2013 at 4:34 Comment(2)
No, it doesn't help. When the views are compiled for some reason it's not aware of my external assembly.Gylys
You load it to the same app domain?Emendate
G
0

I was able to precompile views with RazorGenerator Visual Studio extension (not RazorGenerator.Mvc one) in my assemblies.

It basically converts .cshtml razor views into .cs files with WebViewPage classes before assemblies compile. And in my web project I had to implement my own VirtualPathProviderViewEngine similar to this one

Gylys answered 25/7, 2013 at 22:23 Comment(0)
E
0

can you try...

@using global::Shared.Models;

...on your razorpage

It's works for me, I can access the model imported on my project.

Ecumenicist answered 15/9, 2023 at 10:3 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewTitoism

© 2022 - 2024 — McMap. All rights reserved.