ASP.NET Core Web API 中使用的命名空间应该是什么?

What should be the namespace used in ASP.NET Core Web API?

在我的 ASP.NET Core Web API 项目中,我想使用 HttpPostHttpGetHttpDelete 等方法。当我将其添加到操作中时,IntelliSense 建议使用这三个名称空间:

System.Web.Mvc 
System.Web.Http
Microsoft.AspNetCore.Mvc

应该使用哪一个,为什么?

Microsoft.AspNet.MvcSystem.Web.Mvc完全一样 如果你有 View 支持,那么使用 Mvc,这将支持 Mvc 设计模式

如果不是(您只是在调用第三方应用程序),则使用 System.Web.Http

看看这个=>

这=> For MVC 4, what's the difference between Microsoft.AspNet.Mvc and System.Web.Mvc?

Microsoft.AspNetCore.Mvc 是您在 VisualStudio 中创建 Asp.NET Core Web API 时的默认命名空间。您的项目无需依赖其他名称空间。
您还可以阅读此文档:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc?view=aspnetcore-5.0

例如:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CoreWebApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}