【问题标题】:getting 400 error on webapi call blazorserver在 web api 调用 blazor 服务器上出现 400 错误
【发布时间】:2020-11-22 19:21:07
【问题描述】:

我正在尝试设置一个 blazor 服务器应用程序,调用 webapi。 当我调用 API 时,我不断收到 400 错误。

我有 3 个项目,projectserver 和 projectapi。 projectserver 是 Blazor 应用所在的位置,Project API 是 API 所在的位置。

我不知道 apicall 是否可以找到 API,因为它没有在 API 部分中遇到任何断点,我完全感到困惑,好像它找不到 API 那么它应该返回 404 或其他错误而不是400?

感谢您的努力。 这是我的代码,

Projectserver,这是我将注册模型发布到 API 的地方

    public string message { get; set; }
    public RegisterModel r = new RegisterModel();

    private async Task Create(MouseEventArgs e)
    {
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(r);
        var client = clientfactory.CreateClient("ServerApi");
        var result = await client.PostAsJsonAsync("/Account/Register",json); // check the Startup file and check base address for the Full route. 

        message = result.StatusCode.ToString();
    }
} 

ClientFactory 返回 startup.cs 中定义的基地址

services.AddHttpClient("ServerApi", client => client.BaseAddress = new Uri("https://localhost:44302/"));

API 是 Projectserver,定义如下。

[Route("[controller]")]
    [ApiController]
    public class AccountContoller : ControllerBase
    {
        private readonly ApplicationDbContext _context;
        private readonly SecurityOptions _securityOptions;

        private readonly JwtIssuerOptions _jwtOptions;
        // GET: api/<Account>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<Account>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<Account>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }
        // POST api/<Account>
        [HttpPost("Register")]
        public async Task<ActionResult<RegisterResult>> Register(RegisterModel model)
        {
            RegisterResult r = new RegisterResult();

            var Exisits = await _context.Users.Where(r => r.EmailAddress == model.Email).FirstOrDefaultAsync();

            if(Exisits != null)
            {
                r.Sucsess = false;
                r.ErrorMessage = "Email - Already Exisits";
                return r;
            }
            else
            {
                try
                {
                    User newuser = new User();
                    newuser.CreatedDateTime = DateTime.UtcNow;
                    newuser.UserID = Guid.NewGuid();
                    newuser.MobileNumber = model.MobileNumber;
                    newuser.Password = model.Password;
                    newuser.FirstName = model.FirstName;
                    newuser.Surname = model.LastName;
                    _context.Users.Add(newuser);
                    await _context.SaveChangesAsync();

                    r.Sucsess = true;
                    return r;


                }
                catch(Exception e)
                {
                    r.Sucsess = false;
                    r.ErrorMessage = e.ToString();
                    return r; 
                }
                
                

            }

            
        }

模型类被定义为可序列化

 [Serializable]
    public class RegisterResult
    {
        public bool Sucsess { get; set; }
        public string ErrorMessage { get; set; }
    }
    [Serializable]
    public class RegisterModel
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string RoleID { get; set; }
        public string EntityID { get; set; }
        public string MobileNumber { get; set; }
    }

【问题讨论】:

  • 能否将您的支票张贴在调试控制台中。我怀疑你发送两个斜线。尝试: var result = await client.PostAsJsonAsync("Account/Register",json);

标签: asp.net-core asp.net-web-api blazor blazor-server-side


【解决方案1】:

您能否修改您的代码如下并试一试:-

var serializedBody = JsonConvert.SerializeObject(r);
var jsonRequestBodyContent = new StringContent(serializedBody, Encoding.UTF8,"application/json");
 var client = clientfactory.CreateClient("ServerApi");
 var result = await client.PostAsync("/Account/Register",jsonRequestBodyContent);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 2014-08-06
    • 2017-05-22
    • 1970-01-01
    • 2023-01-15
    相关资源
    最近更新 更多