VS Code 中的 .Net Core MVC "server.Models"缺少引用



我从一个新的开始

dotnet new mvc -o .
dotnet restore

打开Controllers/HomeController.cs时,我看到一个关于引用同一项目中另一个.cs文件的错误。。。

The type or namespace name 'Models' does not exist 
in the namespace 'server' (are you missing an assembly 
reference?) [my-project]

我不确定问题是什么…对.Net Core和通过VS代码使用.Net来说相对较新。

$ dotnet --version
2.1.201

~/控制器/HomeController.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using server.Models;
namespace server.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Error()
{
return View(newErrorViewModel{ RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}

~/Models/ErrorViewModel.cs

using System;
namespace server.Models
{
public class ErrorViewModel
{
public string RequestId { get; set; }
public bool ShowRequestId =>!string.IsNullOrEmpty(RequestId);
}
}

--更新:omniview服务似乎有问题,我用dotnet代码打开了server/目录的父目录的VSCode。

我在HomeController.cs顶部使用dto(数据传输对象(后立即看到了这个错误,例如

using dto = webapplication.Models;

在我的情况下,解决方案是添加前缀dto。到ErrorViewModel,如下所示:

public IActionResult Error()
{
return View(new dto.ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

我认为在您的示例中使用server.Models;也是造成问题的原因。希望这有助于解决问题。

最新更新