Aspnetcore: Razor views cannot declare a custom base type and a model

Created on 18 May 2017  路  5Comments  路  Source: dotnet/aspnetcore

Based on https://github.com/aspnet/Mvc/issues/5397

Currently, the way to specify a custom base type for a view and a model involves specifying the @inherits keyword in a _ViewImports.cshtml:
@inherits MyBaseType<TModel>

and specifying the model in the page:
@model MyModel

Specifying the two in the same page results in a parse error: The 'inherits' keyword is not allowed when a 'model' keyword is used.. Using _ViewImports is limiting here,

a. It does not allow specifying more than one base type per directory
b. not being able to contain all the definition information in a single view file makes it less portable when using our modular framework (e.g. we can have views defined in plugin assemblies)

affected-very-few area-mvc bug severity-nice-to-have

All 5 comments

I don't think there was ever an expectation that you would specify both an @inherits and @model keyword in the same view file. This isn't how MVC5 works and was only mentioned in #5397 when attempting a workaround. In MVC5 you would get the same error: The 'inherits' keyword is not allowed when a 'model' keyword is used.

In MVC5 th correct way to do this is to specify the model type as the generic parameter of the base type e.g. @inherits MyBaseType<MyViewModel> which causes the error in first mentioned in #5397 which I think is covered by #6291.

@pranavkm is this a regression from 1.1.X?

@rynowak no, this isn't a regression from 1.1.x. It is difference from Mvc 5 though.

Running into the same exact issue in #7559. It would be really great you could fix this for the ASP.NET 2.1 release. This is a show-stopping bug for me in Razor Pages.

I'm not looking to run inherits and model in the same view file. I'd like to have a base class that all Razor Pages inherit from set in _ViewImports. Pretty simple. The basic scenario in #7559 should just work.

This issue has been open for ~10 months now. =/

Moving this to the Backlog as it's not a priority for the next release.

Was this page helpful?
0 / 5 - 0 ratings