13

Problem

ASP.NET MVC 3 RC is giving me a compilation error at runtime (when browsing) to an external MVC area. The website itself works, but the plugin refuses to load throwing a compilation issue pertaining to an unknown model.

LogOn.cshtml

@model TestProject.Models.LogOnModel
@{
    View.Title = "Log On";
}
//.....omitted for brevity

Error thrown at runtime.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'model' does not exist in the current context

Source Error:


Line 38:         public override void Execute() {
Line 39: 
Line 40: Write(model);
Line 41: 
Line 42: WriteLiteral(" TestPlugin.Models.LogOnModel\r\n");

Background

The website is an ASP.NET MVC 3 Preview 1 website I have just migrated to ASP.NET MVC 3 RC. I read the release notes and updated accordingly but still encountered an issue with WebMatrix. I found a SO question where someone from the MVC team suggested until RTM we use the following to force the namespaces to be pulled in (I put them in web.config but it didn't work).

namespace WebMatrix.Data { class Foo { } }
namespace WebMatrix.WebData { class Foo { } }

This solved my issue within the website and the website now loads up fine in MVC 3 RC. The problem however, is an external MVC area (separate assembly).

Note I'm not using any third party libraries for this, I wrote a little plugin framework that allows loading an MVC area from another assembly. Views are compiled as embedded resources.

So I have an authentication plugin for example that looks like.

-Controllers
-- AccountController.cs
-Models
-- AccountModels.cs
-Views
--Account
--- LogOn.cshtml
--- ChangePassword.cshtml
--- ...
-Web.config
AccountAreaRegistration.cs
Web.config

Other than the fact that the views are marked as embedded resources, the AccountController extending a PluginController that knows how to load the embedded resouces and the global.asax being removed, its a pretty standard MVC area.

I have tried creating a new empty MVC 3 test plugin project and slowly adding in the relevant code. This ensures all references and web.configs are correct. But I still receive the same model issue described above. The project compiles at design time but throws a compilation issue at runtime when it tries to emit the views it looks.

Just to be clear, this all works fine in ASP.NET MVC 3 Preview 1. However, now that I have upgraded it to MVC 3 RC I have the core website working but the external area refuses to work.

There is nothing wrong the the plugin framework itself. It works fine in MVC 3 Preview 1. I'm looking for answers that might shed a little bit of light on what may be occuring in MVC 3 RC and why I might be using receiving this model error.

Any ideas?

Update

Interestingly if I change the declaration of the model at the top of the view to Preview 1 syntax `@inherits System.Web.Mvc.WebViewPage

I get a different runtime compiler error. This time I get an error just further down when the Html.ValidationSummary begins for the login form.

@inherits System.Web.Mvc.WebViewPage<TestProject.Models.LogOnModel>
@{
    View.Title = "Log On";
}
<div class="content_item half loginform">
    <div class="content_body">
        <h1>VastID Login</h1>
        @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") // <=== Compilation issue here on Html.ValidationSummary
        @using (Html.BeginForm())
        {
          // .. omitted for brevity
        }
    </div>
</div>
Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ValidationSummary' and no extension method 'ValidationSummary' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 49: 
Line 50: 
Line 51:    Write(Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again."));
Line 52: 
Line 53: WriteLiteral("\r\n");

It's interesting that there is no compile time errors. System.Web.WebPages, System.Web.Helpers and all the necessary MVC 3 RC dll's are present. Including the necessary web.config's. It's as if these aren't available when the view is trying to be loaded however.

The actual razor syntax I believe is part of System.Web.WebPages? Where does Html.ValidationSummary live? System.Web.Mvc or System.Web.Helpers?

7
  • What does the plugin/area source code look like? It looks like it's referencing a variable called "model" that doesn't exist.
    – Eilon
    Nov 12, 2010 at 2:29
  • It's the start of the razor view, strong typing the view's model to a logon model. I will update question to clarify this. Nov 12, 2010 at 2:34
  • What are you using to get the CSHTML file to be compiled?
    – Eilon
    Nov 12, 2010 at 2:40
  • I'm not sure I understand this question? Nov 12, 2010 at 2:42
  • It looks like your CSHTML file is being compiled as though there's no "@model" keyword and that it actually thinks you want to render a variable named "model" - sort of the opposite of what I said in my actual answer below. If your CSHTML file is under the "Views" folder, which contains a special web.config with MVC-specific Razor settings, then it should know that "@model" is a keyword and not a variable being rendered.
    – Eilon
    Nov 12, 2010 at 2:44

4 Answers 4

9

I actually discovered that an ASP.NET MVC 3 RC web.config was missing from my /Plugins folder. This is an empty folder other than a web.config that must be present for the plugin virtual path provider. This was the missing link.

Basically what happens is that you have a controller which extends PluginController. This controller alters the path to the views and appends 'Plugin' and the assembly name (remeber we are dealing with embedded views here). So you end up with something like

/Plugins/TestProject.dll/TestProject.Views.Account.LogOn.cshtml

The virtual path provider and collective classes then take care of finding and returning this view from the plugins resource manifest.

What had me really stumped was a breaking change between Preview 1 and Beta where they added an _ViewStart.cshtml. When a view such as Logon.cshtml was requested from the plugin an internal request by the framework was being made for a _ViewStart file which did not exist in that assembly. As execution was already in the virtual path provider at that point I couldn't find out to return the _ViewStart.cshtml from the website. So instead I added one to the plugin.

I had to make alterations to both the PluginVirtualFile.cs and the PluginProvider.cs. So I got it all working in the end.

Thanks very much for your help though @Eilon. Though it didn't arrive on the answer it was greatly appreciated.

3
  • 1
    Thanks for posting. I had the same problem with the mvc portable areas when upgrading.
    – Victor
    Dec 20, 2010 at 2:31
  • 2
    It would be a big help if you could explain in more detail how you solved this problem. I have the same exact issue. I added the _ViewStart.cshtml to my External Plugin Project, and I get new errors, depending on what I put in the ViewStart file. The first error I got was that it could not cast my _ViewStart.cshtml as type 'System.Web.WebPages.StartPage'. Any help would be appreciated!Thanks!
    – user604892
    Feb 6, 2011 at 20:42
  • Similar to @JoshuaHayes I had to create an empty plugins folder containing both web.config and _ViewStart.cshtml. I then modified my "embedded" virtual path provider to ignore any paths containing _ViewStart.
    – Ben Foster
    Mar 17, 2012 at 12:10
7

It looks like your CSHTML file is being compiled as though there's no @model keyword and that it actually thinks you want to render a variable named model.

If your CSHTML file is under the Views folder, which contains a special web.config with MVC-specific Razor settings, then it should know that @model is a keyword and not a variable being rendered. Please check your ~/Views/web.config file and make sure it has the <system.razor.web> section and the right settings for the MVC Razor host.

4
  • Yes the ~/Views/web.config was copied from the views folder of a clean MVC 3 RC Views folder. It has the appropriate <system.razor.web> section. I found without this the project wouldn't actually compile correctly and I would get these sorts of issues at compile/design time. Nov 12, 2010 at 2:53
  • It looks like you have a new error listed now. Did you check that the web.config section also includes the default MVC namespaces, such as System.Web.Mvc.Html?
    – Eilon
    Nov 12, 2010 at 3:00
  • Yup as it's a completely standard ~/Views/web.config it has all those. and more; System.Web.Mvc, System.Web.Mvc.Ajax, System.Web.Mvc.Html, System.Web.Routing etc. Nov 12, 2010 at 3:05
  • I reverted the @model declaration at the top of the view to the Preview 1 syntax (as this still seems to be supported?) just to see if I could get past it / move on to a different error. Which I did. It's as if asp cannot find the dll's at runtime. Nov 12, 2010 at 3:13
2

I had this .. and it was I'd not deployed my web.config in either the route or the Views folder.

(I was using Beyond Compare to deploy and I'd got web.config filtered out, hence it was missed)

2
  • It would be great if you can elaborate on this as for someone who has explained his question with such details, answering with more clarity would be welcome as similar situation may arise for someone in future and might need more context to understand your solution :)
    – Amar
    Nov 6, 2012 at 17:35
  • Missing Web.config in Views folder that caused this error for me.
    – Duncan
    Jan 24, 2013 at 22:51
0

Was missing this in my case: Had system.web.webPages.razon in configSections, but missed the relative section.

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.