Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

ViewModel's aren't found when they are in an external assembly alongside an embedded View. #4100

Closed
dazinator opened this issue Feb 15, 2016 · 1 comment

Comments

@dazinator
Copy link

Simple repro here: https://github.com/dazinator/mvc6-view-components/commits/issue-noprojectreferences - just run the project and browse to url /Books

The issue is that when you place your Razor View in an external assembly, and you also create a Model class in that assembly, and in your main application you create an embedded file provider to allow the view to be resolved, MVC is still not able to resolve it's Model despite the fact its in the same assembly as the view. There doesn't seem to be any obvious API that can be called to tell MVC that the Model is in the same assembly as the embedded view. Creatiing an IAssemblyProvider that returns the assembly containing the model in the list of candidate assemblies also does not fix the problem. How does MVC 6 resolve the list of assemblies to compile the Razor View against and how to we get it to include the assembly with the model?

image

Any ideas?

@dazinator dazinator changed the title ViewModel's don't work when they are in an external assembly. ViewModel's aren't found when they are in an external assembly alongside an embedded View. Feb 15, 2016
@danroth27 danroth27 added this to the Backlog milestone Feb 17, 2016
@dazinator
Copy link
Author

Solution in RC2 is to ensure the reference is included in the Razor CompilationOptions.

            var mvcBuilder = services.AddMvc();
            mvcBuilder = mvcBuilder.AddRazorOptions((options) =>
            {

                var previous = options.CompilationCallback;
                options.CompilationCallback = (compilationContext) =>
                {
                    if (previous != null)
                    {
                        previous(compilationContext);
                    }

                    var references = additionalAssemblies.Select(a => MetadataReference.CreateFromFile(a.Location));
                    compilationContext.Compilation = compilationContext.Compilation.AddReferences(references);
                };              

                // also add additional file providers to allow razor views to be found from elsewhere..
                //  options.FileProviders.AddRange(_);
            });

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants