jonahxuworld
2022-04-11 13:35
采纳率: 95.7%
浏览 26

C#winform调用web api出现400的问题,请问如何解决?

具体错误如图!

img

这是我的控制器

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using PopUpWebApi.Models;

namespace PopUpByMessage.Controllers
{
    [Route("api/MyWebApi")]
    [ApiController]
    public class MyWebApiController : ControllerBase
    {
        private readonly ILogger<MyWebApiController> _logger;
        public MyWebApiController(ILogger<MyWebApiController> logger)
        {
            _logger = logger;
        }
        //[HttpPost("[action]")]
        //public IActionResult SayHello([FromBody] UserModel model)
        //{
        //    return new JsonResult($"Hello {model.UserName}");
        //}
        [HttpPost("[action]")]
        public IActionResult SayHello([FromBody] UserModel model)
        {
            if (model.pwd != "1")
            {
                object obj = new
                {
                    code = "0",
                    msg = "请求失败"
                };
                return new JsonResult(obj);
                //return new BadRequestResult();
            }
            else
            {
                object obj = new
                {
                    code = "1",
                    msg = "请求成功"
                };
                return new JsonResult(obj);
            }
        }
    }
}


1条回答 默认 最新

相关推荐 更多相似问题