我正在使用VS代码创建一个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]发布于 2018-05-29 18:31:27
你把技术混在一起。System.Web命名空间及其子名称空间引用ASP.NET 4.x框架,您将创建一个ASP.NET核心应用程序。
您要查找的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();
}
}发布于 2018-05-29 18:33:09
命名空间System.Web.Mvc用于:NET中的ASP.NET。在ASP.NET核心中,相应的命名空间是Microsoft.AspNetCore.Authorization。有关更多细节,请查看正式文件。
https://stackoverflow.com/questions/50590712
复制相似问题