Thursday 21 July 2016

razor - 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.


Answer



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:



...


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




...


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:




Library



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...