httpdelete请求asp.net核心2中的问题

时间:2019-03-21 12:39:15

标签: asp.net-core-2.2

您好,我正在使用asp.net core 2.2 Web api,我的get请求工作正常,但是我在HTTPDELETE请求中遇到问题,删除请求的代码如下

 [Route("api/[controller]")]
[ApiController]
public class PatientController : ControllerBase
{
    IPatientManager _patientManager;
    IEnumerable<Patient> patientList;

    public PatientController(IPatientManager patientManager)
    {
        _patientManager = patientManager;
    }

    [HttpGet]
    public IEnumerable<Patient> Get()
    {
        return (patientList = _patientManager.GetAllPatients());
    }


    // DELETE api/values/5
    [HttpDelete("api/Patient/{id}")]
    public bool Delete(long id)
    {
        if (_patientManager.DeletePatient(id))
            return true;
        else
            return false;

    }
}

}

当我将请求以localhost:3的形式放置在URL中时,n922 / api / Patient / 444373会给我HTTP错误404,我的startup.cs文件正在使用下面的MapRoute代码

 app.UseMvc(opt =>
            {
                opt.MapRoute("Default",
                    "{controller=Patient}/{action=Get}/{id?}");
            });

要启动我的PatientController而不是ValuesController,请帮助我做错了什么问题? TIA

1 个答案:

答案 0 :(得分:0)

您在控制器上将路由前缀定义为api/[controller],它转换为/api/Patient。然后,将您执行操作的路线定义为api/Patient/{id},这将使该操作的完整路线为:/api/Patient/api/Patient/{id}。显然这是不对的,它是404的来源。将路由更改为{id}

[HttpDelete("{id}")]
public bool Delete(long id)