Post 方法在 ASP.NET 核心中不起作用

Post Method is not working in ASP.NET Core

我正在尝试创建 ASP.NET 核心网站 API。我有 Post/Get 方法如下:

    [HttpGet]
    [Route("api/getProffessionByID/{id}")]
    public IHttpActionResult getProffessionByID(Int64 id)
    {
        AllProffession model = new AllProffession();
        try
        {
            var result = model.GetAllProffession.Where(i => i.id == id).FirstOrDefault();
            return Ok(result);
        }
        catch (Exception ex)
        {
            return InternalServerError("Something Went Wrong : " + ex.ToString());
        }
    }

    [HttpPost]
    [Route("api/savePerson")]
    private IHttpActionResult savePerson(PersonModel model)
    {
        try
        {
            if (model.name != string.Empty || model.weight != 0 || model.height != 0 || model.proffession != 0)
            {
                Guid guid = Guid.NewGuid();
                Random random = new Random();
                int i = random.Next();
                model.id = i;
                return Ok(model);
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return InternalServerError("Something Went Wrong : " + ex.ToString());
        }
    }

我的 get 方法在 url 和 Postman 中都运行良好。

我也试过[FromBody],但还是不行

型号:

public class PersonModel
    {
        public int id { get; set; }
        public string name { get; set; }
        public int weight { get; set; }
        public int height { get; set; }
        public int proffession { get; set; }
    }

WebApiConfig.cs

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { Controllers = "API", id = RouteParameter.Optional }
            );

            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
        }

也许您应该将方法更改为 public 而不是 private.

[HttpPost]
[Route("api/savePerson")]
public IHttpActionResult savePerson(PersonModel model) {
    //...
}