Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name 'model' does not exist in current context in ASP.NET Core

I'm trying to develop a website with plugins using ASP.NET Core. I have my main project, asp.net web application which works fine. I have a class library project with controllers which also works fine and views which I have a problem with. When I open *.cshtml file I can see that Razor doesn't work - @using, @model and other directives are not recognized and intellisense doesn't work. When I hover over model I'm given the error from title:

The name 'model' does not exist in current context in ASP.NET Core

I use EmbeddedFileProvider for discovering views and they're marked as Embedded resource. When I run application everything works - views are rendered correctly. My only issue is false-positive errors in Visual Studio. I googled and there similar issues, but mainly for ASP.NET MVC, not Core - like here. I think something is missing in class library configuration, but I'm not sure what exactly.

like image 497
Twelve Avatar asked Apr 19 '17 14:04

Twelve


1 Answers

The new csproj file has an "Sdk" attribute at the top level "Project" element. When you create a new Web application, this gets set to the "Web" SDK:

<Project Sdk="Microsoft.NET.Sdk.Web">...</Project>

When you add/create a class library, it defaults to the standard non-web SDK:

<Project Sdk="Microsoft.NET.Sdk">...</Project>

As far as I can tell, the Web SDK imports additional tasks to allow design-time processing of Web resources, such as Razor views. Change the SDK in your plugin library to Web.

Another parameter of a "Web" project is that it outputs an EXE by default, so also add this to your plugin csproj:

<PropertyGroup>
    <OutputType>Library</OutputType>
</PropertyGroup>
like image 93
Dave Thieben Avatar answered Oct 02 '22 08:10

Dave Thieben