我使用VS Code创建一个asp.net项目。首先提出这个问题是因为我需要使用AllowAnonymous操作过滤器。它给了" The type or namespace name 'AllowAnonymousAttribute' could not be found.
"所以我添加了使用System.Web.Mvc;在开始部分。它显示了error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web'
你能给出任何建议吗?感谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using DatingApp.API.Data;
using Microsoft.EntityFrameworkCore;
namespace DatingApp.API.Controllers
{
[AllowAnonymous]
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly DataContext _context;
public ValuesController(DataContext context)
{
_context = context;
}
// GET api/values
[HttpGet]
public async Task<IActionResult> GetValues()
{
var values=await _context.Values.ToListAsync();
return Ok(values);
}
.......
运行dotnet watch run
后,它会出现如下错误。
watch : Started
Controllers\ValuesController.cs(4,18): error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) [C:\Xumin\projects\DatingApp\DatingApp.API\DatingApp.API.csproj]
Controllers\ValuesController.cs(12,6): error CS0246: The type or namespace name 'AllowAnonymousAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Xumin\projects\DatingApp\DatingApp.API\DatingApp.API.csproj]
Controllers\ValuesController.cs(12,6): error CS0246: The type or namespace name 'AllowAnonymous' could not be found (are you missing a using directive or an assembly reference?) [C:\Xumin\projects\DatingApp\DatingApp.API\DatingApp.API.csproj]
答案 0 :(得分:0)
你正在混合技术。 System.Web
命名空间及其子命令引用ASP.NET 4.x框架,您正在创建ASP.NET Core应用程序。
The AllowAnonymousAttribute类位于Microsoft.AspNetCore.Authorization
命名空间:
namespace Microsoft.AspNetCore.Authorization
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class AllowAnonymousAttribute : Attribute, IAllowAnonymous
{
public AllowAnonymousAttribute();
}
}
答案 1 :(得分:0)
名称空间System.Web.Mvc
适用于.NET Framework中的ASP.NET。在ASP.NET Core中,相应的命名空间为Microsoft.AspNetCore.Authorization
。查看official documentation了解详情。