名称“模型"在 ASP.NET Core 的当前上下文中不存在 [英] The name 'model' does not exist in current context in ASP.NET Core

查看:33
本文介绍了名称“模型"在 ASP.NET Core 的当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ASP.NET Core 开发一个带有插件的网站.我有我的主要项目 asp.net web 应用程序,它运行良好.我有一个带有控制器的类库项目,它也可以正常工作,而视图却有问题.当我打开 *.cshtml 文件时,我可以看到 Razor 不起作用 - @using、@model 和其他指令无法识别并且智能感知不起作用.当我将鼠标悬停在模型上时,我收到了来自标题的错误:

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:

名称模型"在 ASP.NET Core 的当前上下文中不存在

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

我使用 EmbeddedFileProvider 来发现视图,它们被标记为嵌入式资源.当我运行应用程序时,一切正常 - 视图正确呈现.我唯一的问题是 Visual Studio 中的误报错误.我用谷歌搜索了类似的问题,但主要是针对 ASP.NET MVC,而不是核心 - 像这里.我认为类库配置中缺少某些内容,但我不确定到底是什么.

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.

推荐答案

新的 csproj 文件在顶级Project"元素中有一个Sdk"属性.当您创建一个新的 Web 应用程序时,它会被设置为Web"SDK:

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>

当您添加/创建类库时,它默认为标准的非 Web SDK:

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

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

据我所知,Web SDK 导入了额外的任务以允许在设计时处理 Web 资源,例如 Razor 视图.插件库中的 SDK 更改为 Web.

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.

Web"项目的另一个参数是它默认输出一个 EXE,因此也将其添加到您的插件 csproj 中:

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>

这篇关于名称“模型"在 ASP.NET Core 的当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆