O.O
O.O

Reputation: 11287

Type 'ASP._Page_index_cshtml' does not inherit from 'System.Web.WebPages.WebPage'

I am getting:

Type 'ASP._Page_index_cshtml' does not inherit from 'System.Web.WebPages.WebPage'.

when I browse to my index.cshtml file. It is very simple:

@using System.Web.Optimization
@inherits System.Web.Mvc.WebViewPage
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width">
    <title>Hello World</title>
    @Styles.Render("~/Content/css", "~/Content/themes/base/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @Scripts.Render(
        "~/bundles/jquery",
        "~/bundles/jqueryui"
    )
</body>
</html>

my index.cshtml file is outside of the Views folder, if that matters at all.

Upvotes: 9

Views: 24015

Answers (10)

Abdus Salam Azad
Abdus Salam Azad

Reputation: 5502

Comment Layout variable in your .cshtml file

//Layout = "~/Views/_ViewStart.cshtml";

Upvotes: 0

Aron Boyette
Aron Boyette

Reputation: 957

What worked for me was to add...

@inherits System.Web.Mvc.ViewStartPage

...to the top of _ViewStart.cshtml.

I think the reason this worked for me was because of my ~/Views/Web.config file. In it I had placed the following block in order to avoid a bunch of @inherits calls on my views...

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage"> <!-- Notice pageBaseType -->
      ...
    </pages>
  </system.web.webPages.razor>
</configuration>

I think this instructed the razor engine that all views including _ViewStart.cshtml were to be of type System.Web.Mvc.WebViewPage. This is not the same type as System.Web.Mvc.ViewSTartPage and hence the error. Putting @inherits System.Web.Mvc.ViewStartPage at the start of _ViewStart.cshtml explicitly specifies the required type for _ViewStart.cshtml.

Upvotes: 1

Chesare
Chesare

Reputation: 383

I change the cshtml line from:

Layout = "~/Views/_ViewStart.cshtml";

to

Layout = "~/Views/Shared/_Layout.cshtml";

since _ViewStart.cshtml contains only that line of code. Layout file was required because it contains the script to client-side validation.

It worked also when I remove the line, but I prefer to keep _Layout.cshtml included.

Upvotes: 2

sourabh
sourabh

Reputation: 1

No need to take Tension. Just follow 2 basic steps

  1. Delete the _ViewStart.cshtml
  2. Make statement to a comment. Means for the following statement in index view:

    Layout = "~/Views/_ViewStart.cshtml";
    

    and mark it as a comment:

    //Layout = "~/Views/_ViewStart.cshtml";
    

Upvotes: 0

Alexandre Moreira
Alexandre Moreira

Reputation: 11

Just delete the _ViewStart.cshtml and re-add.

Upvotes: 0

Yogurtu
Yogurtu

Reputation: 3031

Do not remove the inheritance, it might be necesary and might lead to another problems in the future.

Instead try this:

Enable "View all Files" in the web project that is failing, and search for a file that seems correct but is not included in visual studio, and delete it. If it fails in your deployment folder, try to clean the folder as well, and re-deploy the site, you might have unnecesary files that might cause the same problem.

In my case, at the root of the webproject I had an extra copy of _ViewStart.cshtml (excluded from the project), I deleted that file, and that did the trick.

Hope it helps, let me know if this solve your problem as well.

Upvotes: 4

Saurin Vala
Saurin Vala

Reputation: 1928

Remove the web.config from your Views folder.

As you're including Partial1.cshtml from that folder, it is also including the web.config from within there. And that web.config is saying that all pages must inherit from WebViewPage.

Upvotes: -4

Dmitry Dzygin
Dmitry Dzygin

Reputation: 1308

It could be that an older version of System.Web.WebPages.dll is loaded to memory, and it tries to cast the your cshtml page to a version of WebPages class from that dll.

To test this, try to see what http modules are currently registered:

var allModules = HttpContext.Current.ApplicationInstance.Modules;
for( int i = 0; i < allModules.Count; i++ ) {
    Trace(allModules.GetKey(i));
}

In my case that was:

....
__DynamicModule_System.Web.WebPages.WebPageHttpModule, System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35_bca8e05a-5746-45b0-be95-2b920b455ccf

__DynamicModule_System.Web.WebPages.WebPageHttpModule, System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35_c1a67b42-31a9-47f1-8483-9e712fabe2a7

To fix the problem you need to replace the older version of System.Web.WebPages.dll in your /Bin folders, or some other dll-s that might be referencing it.

Upvotes: 0

O.O
O.O

Reputation: 11287

I just had to remove: @inherits System.Web.Mvc.WebViewPage

Looks like I had a copy paste error when reorganizing my project.

Upvotes: 2

rybl
rybl

Reputation: 1679

Why are you trying to browse directly to a view? And why isn't it in a views folder?

To get a basic "Hello World" page up you first want to create a controller called HomeController.cs:

using System.Web.Mvc;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Then create a file /Views/Home/Index.cshtml and put your markup in that. You may also need to add:

@{
    Layout = null;
 }

to the top of the page since it doesn't look like you are using a master page.

As a side note, all this assumes you haven't screwed with your default routing.

Upvotes: -1

Related Questions