75

I'm following Justin Slattery's Plugin Architecture tutorial and trying to adapt it for Razor, instead of WebForm Views.

Everything else (controllers, plugin assembly loading, etc) seems to be okay. However, I'm not able to get embedded Razor views to work properly. When I try to browse to the "HelloWorld/Index", I get the following error:

The view at '~/Plugins/MyProjectPlugin.dll/MyProjectPlugin.Views.HelloWorld.Index.cshtml' must derive from WebViewPage or WebViewPage<TModel>.

The exception is thrown by System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +262

I can include the complete stack trace, if needed.

Can anyone advise as to what I might be doing wrong?

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Nasir
  • 10,935
  • 8
  • 31
  • 39

7 Answers7

163

You may checkout the following blog post which is more adapted to Razor.

But to answer your question, since you are now serving your views from a non standard location there is no longer the ~/Views/web.config file that applies and allows you to specify the base type for your razor views. So you might need to add the following on the top of each razor view:

@inherits System.Web.Mvc.WebViewPage
@model ...
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Thank you for both pieces of information. I will check out the blog you mentioned. – Nasir Nov 14 '11 at 20:24
  • 13
    I was able to copy the web.config from ~/Views to my alternate location to solve my issue with this. Thanks. – joelnet Apr 23 '12 at 02:09
  • 2
    @Darin Dimitrov I have a similar problem. When I try adding `@inherits` I am getting an error saying that I can't use `@inherits` and `@model` at the same time. Can you help with this? EDIT. I am using mvc4 – gumenimeda Sep 03 '13 at 16:29
  • 2
    If you're having trouble getting the solution in @joelnet's comment to work, try cleaning and rebuilding the project. – Paul d'Aoust Sep 24 '13 at 18:19
  • 5
    @DarinDimitrov: I created a sample application and in shared folder I created a View sampleView.html for sending emails . I got the above crash and used the above code and now it's saying error **The 'inherits' keyword is not allowed when a 'model' keyword is used.** – Imad Alazani Dec 28 '13 at 23:23
  • @Imad Alazani: `@model myModelType` => means: `@inherits WebViewPage` – S.Serpooshan Sep 28 '16 at 10:54
6

Note: I had an extremely similar issue and had to finally track down that I was the only developer with MVC 4.0.0.0 linked in their solution.

Everyone else had 3.0.0.0 properly referenced and was building/running without error.

TL;DR Make sure that your references are the same as the rest of your development group.

nolsen311
  • 101
  • 1
  • 5
1

I had a same problem because I did not commit the packages folder which contains the razor libraries. Then I added the package, it worked but upon rebuild it broke. Then I turned off nuget automatic run upon build and now it's working fine. In my case, nuget was messing things up.

max
  • 9,708
  • 15
  • 89
  • 144
0

Just got this error today when updating a old MVC3-project to version 4 and for me the solution was to remove the attribute appliesTo="v2.0.50727" in the <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> section.

Fr3gU
  • 57
  • 1
  • 5
0

I want to share my pain.. site was working fine locally, however when I published to customer's IIS, it was throwing this error. All the web.config files (root and /Views) matched perfectly with a working copy of the site.

One of the views (logon.cshtml) had inconsistent line endings and I didn't click yes to fix it on my Windows machine.

Clicking yes to the inconsistent line endings dialog and publishing the logon.cshtml view fixed it for me.

Noobie3001
  • 1,230
  • 18
  • 31
0

I got this error when I tried to add MVC scaffolding (specifically the Views folder) to a Web API project. Darin Dimitrov's answer says:

there is no longer the ~/Views/web.config file that applies and allows you to specify the base type for your razor views

And in comments on that answer, joelnet said:

I was able to copy the web.config from ~/Views to my alternate location to solve my issue with this.

and Paul d'Aoust said:

If you're having trouble getting the solution in @joelnet's comment to work, try cleaning and rebuilding the project.

These hints helped me figure out the solution.

First I copied the Web.config from the Views folder of another project in the same solution to my new Views folder. I renamed the main namespace tag to match the new project, and after getting an error removed another <add namespace="..." /> that I didn't need. I then triggered a build (only a normal build, I didn't need to clean and rebuild), and I was able to load my new view without errors.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
-1

Install the package: Install-Package Microsoft.AspNet.WebPages

  • 1
    I don't believe `Microsoft.AspNet.WebPages` package was available 4 years ago for ASP.NET MV3. – Nasir Apr 05 '16 at 22:41
  • 2
    @Nasir, I'm asking genuinely, is it preferred on StackOverflow that the answer pertain to the technology that was available at the time of posting even if the problem still persists today? – Jared Beach May 11 '16 at 20:30
  • 1
    my personal opinion is that if you're answering and old question, at least acknowledge that answer being provided may not have been relevant when the question was originally asked. Otherwise, it comes off as either an uninformed answer or that the user didn't pay attention to the details (date being one of those details). – Nasir May 12 '16 at 01:19