错误CS0234:类型或命名空间名称' Mvc'名称空间中不存在System.Web' (你错过了一个' System.Web'

时间:2018-05-29 18:26:38

标签: c# asp.net-core

我使用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]

2 个答案:

答案 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了解详情。