Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core 无法访问asp.net core webapi中的删除终结点_Asp.net Core_Asp.net Core Webapi - Fatal编程技术网

Asp.net core 无法访问asp.net core webapi中的删除终结点

Asp.net core 无法访问asp.net core webapi中的删除终结点,asp.net-core,asp.net-core-webapi,Asp.net Core,Asp.net Core Webapi,我有.net核心web api,能够访问get端点,但不能访问delete端点。如何访问删除端点。我不确定是什么问题 我试过以下方法 我一直在使用get端点 控制器 public class CitiesController : Controller { private readonly ICityInfoService _cityInfoService; public CitiesController(ICityInfoService cityInf

我有.net核心web api,能够访问get端点,但不能访问delete端点。如何访问删除端点。我不确定是什么问题

我试过以下方法

我一直在使用get端点

控制器

 public class CitiesController : Controller
    {
        private readonly ICityInfoService _cityInfoService;

        public CitiesController(ICityInfoService cityInfoService)
        {
            _cityInfoService = cityInfoService;
        }

        [HttpGet]
        public IActionResult GetCities()
        {
            var cities = _cityInfoService.GetCities();

            if (!cities.Any())
                return NoContent();

            var citiesMapped = cities.Select(MapCity);

            return Ok(citiesMapped);
        }

        [HttpGet("{cityId:int}")]
        public IActionResult GetCity(int cityId)
        {
            try
            {
                var city = _cityInfoService.GetCity(cityId);
                var cityMapped = MapCity(city);

                return Ok(cityMapped);
            }
            catch (CityNotFoundException e)
            {
                return BadRequest(e.Message);
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }

        [HttpDelete("delete-city/{cityId:int}")]
        public IActionResult DeleteCity(int cityId)
        {
            try
            {
                _cityInfoService.DeleteCity(cityId);

                return Ok();
            }
            catch (CityNotFoundException e)
            {
                return BadRequest(e.Message);
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }

        private static CityDto MapCity(City city)
        {
            return new CityDto
            {
                Id = city.Id,
                Description = city.Description,
                Name = city.Name
            };
        }
    }

您的delete方法是HttpDelete,因此需要删除请求。如果您试图通过浏览器点击端点,它将无法工作,因为它只会执行GET

您可以使用Curl或Postman向URL发出删除请求


你的路由配置是什么?如果你在本地主机上使用IIS?我试图通过postman访问url,但它不起作用。我刚刚用postman中删除请求的照片更新了我的答案,你使用了吗?