Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create the own VirtualPathProvider in MVC4?

I'm suffering trying to get some views from a library to the main project. I was starting to read about creating your own VirtualPathProvider implementation here: Using VirtualPathProvider to load ASP.NET MVC views from DLLs

I had to set my view = EmbbebedResource to get the resource from the library. But now is throwing another error.

In the header of my partial view I had the following:

@model Contoso.ExercisesLibrary.AbsoluteArithmetic.Problem1

And the error says: External component has thrown an exception. c:\Users\Oscar\AppData\Local\Temp\Temporary ASP.NET Files\root\4f78c765\7f9a47c6\App_Web_contoso.exerciseslibrary.absolutearithmetic.view1.cshtml.38e14c22.y-yjyt6g.0.cs(46): error CS0103: The name 'model' does not exist in the current context

I don't know why the compiler tells that cannot recognized my model. When I'm in design mode, I can see the compiler that the check is all right.

Check the image

enter image description here

What am I doing wrong o what am I missing? Thanks in advance.

like image 252
Darf Zon Avatar asked Jan 14 '23 14:01

Darf Zon


2 Answers

Try adding an @inherits directive to the top of your razor view:

@inherits System.Web.Mvc.WebViewPage
@model Contoso.ExercisesLibrary.AbsoluteArithmetic.Problem1

The reason you need this is because your view comes from an embedded resource and not from the standard ~/Views location. And as you know inside ~/Views there's a file called web.config. And inside this file there's a pageBaseType="System.Web.Mvc.WebViewPage" directive indicating that all Razor files inside ~/Views should inherit from this base type. But since your view is now coming from an unknown location you have nowhere specified that it should be a System.Web.Mvc.WebViewPage. And all the MVC specific stuff such as models, HTML helpers, ... are defined in this base class.+

like image 191
Darin Dimitrov Avatar answered Jan 18 '23 23:01

Darin Dimitrov


I faced this issue "The name 'model' does not exist in the current context". What I did was added same "areas" folder structure (from my embedded mvc project) to my main MVC project (Areas/AnualReports/Views/) and copied web.config (default web.config from views folder, not the one from root) to Views folder which solved the issue. I am not sure this will work in your case.

Update: Adding web.config (from views folder) to root "areas" folder in main MVC project also works.

like image 45
dotNet Decoder Avatar answered Jan 19 '23 01:01

dotNet Decoder