类型或名称空间名称“托管"在名称空间"Microsoft.AspNetCore.Razor"中不存在 [英] The type or namespace name 'Hosting' does not exist in the namespace 'Microsoft.AspNetCore.Razor'
问题描述
我已经使用Visual Studio .NET Core 2.1 SDK + React模板创建了一个项目.
I've created a project using the Visual Studio .NET Core 2.1 SDK + React Template.
在运行项目时遇到以下错误:
I'm met with the following error when running the project:
缺少一个或多个编译参考.确保您的项目引用的是"Microsoft.NET.Sdk.Web",并且"PreserveCompilationContext"属性未设置为false.
One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.
名称空间中不存在类型或名称空间名称托管" "Microsoft.AspNetCore.Razor"(您是否缺少程序集引用?)
The type or namespace name 'Hosting' does not exist in the namespace 'Microsoft.AspNetCore.Razor' (are you missing an assembly reference?)
我已经做了我能做的研究,并且尝试了以下尝试:
I've done what research I could and have tried the following to no avail:
-
程序包管理器控制台中的
-
dotnet restore
- 删除我的
bin
和obj
文件夹 - 打开
_ViewImports.cshtml
并为解决方案中的每个命名空间添加@using
dotnet restore
in Package Manager Console- Deleting my
bin
andobj
folders - Opening
_ViewImports.cshtml
and adding a@using
for every namespace in my solution
如果我将HomeController Index()
从返回的View()
修改为[HttpGet] public string Index() => "Hello World!";
,则返回的文本没有任何错误.
If I modify the HomeController Index()
from returning the View()
to instead [HttpGet] public string Index() => "Hello World!";
, the text is returned without any error.
Index.cshtml
@{
ViewData["Title"] = "Home Page";
}
<div id="react-app">Loading...</div>
@section scripts {
<script src="~/dist/main.js" asp-append-version="true"></script>
}
HomeController.cs
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
namespace sample_project.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Error()
{
ViewData["RequestId"] = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
return View();
}
}
}
推荐答案
发布此消息后不久,我已经解决了该问题.
Shortly after posting this I've solved the issue.
我已将项目升级到.NET Core 2.1,但其中一个引用(Microsoft.AspNetCore.All
)是版本2.0.8
.
I had upgraded my project to .NET Core 2.1 but one of the references (Microsoft.AspNetCore.All
) was version 2.0.8
.
将其更新为2.1.0
后,该项目现在可以按预期工作.
Upon updating this to 2.1.0
, the project now works as expected.
这篇关于类型或名称空间名称“托管"在名称空间"Microsoft.AspNetCore.Razor"中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!