webapi调用blazorserver时出现400错误



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

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

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

谢谢你的努力。这是我的代码,

项目服务器,这是我将注册模型发布到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 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);

相关内容

  • 没有找到相关文章

最新更新