Azure ASP.NET Core POST 400 错误请求 Blazor Webassembly
Azure ASP.NET Core POST 400 bad request Blazor Webassembly
我一直在尝试在 Azure 应用服务上部署我的 asp.net 核心托管 blazor webassembly 应用程序,但是,我无法让 api 工作。当我尝试将用户数据保存在数据库中时,出现 400 错误请求错误。它在本地主机上运行良好。我环顾四周,发现建议我使用 Azure 中的日志流来获取更详细的错误消息,尽管我不确定这些细节是否真的有帮助,但还是在这里。
2020-06-22 22:24:54 MYPROJECT POST /api/Register/Post X-ARR-LOG-ID=ef27263e-dead-417a-b136-89a217a6f931 443 - MYIP Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/83.0.4103.97+Safari/537.36 ARRAffinity=7f74b113b9ae575a087ae1fa07a63858e6e34f27224b7aa1a957b06074e65ffd https://myproject.azurewebsites.net/Register
相关申请代码如下:
//Register.razor in client project
@code {
private RegisterModel Model = new RegisterModel();
private bool ShowErrors;
private List<string> Errors = new List<string>();
private async Task HandleValidSubmit()
{
ShowErrors = false;
Errors.Clear();
if (Model.Password.Length >= 6 && Model.Password == Model.ConfirmPassword)
{
await HttpClient.PostAsJsonAsync<RegisterModel>("api/Register/Post", Model);
NavigationManager.NavigateTo("/");
}
else
{
if (Model.Password.Length > 100 || Model.Password.Length < 6)
{
Errors.Add("Password must be between 6 and 100 characters in length.");
}
if (Model.Password != Model.ConfirmPassword)
{
Errors.Add("Passwords do not match.");
}
ShowErrors = true;
}
}
}
//RegisterController.cs in server project
[Route("api/Register")]
public class RegisterController : Controller
{
private UserContext UserContext { get; set; }
private IHasher Hasher = new Pbkdf2Hasher();
public RegisterController (UserContext userContext)
{
UserContext = userContext;
}
[RequireHttps]
[HttpPost]
[Route("Post")]
public async Task Post([FromBody]RegisterModel model)
{
var user = new UserModel
{
Email = model.Email,
Password = Hasher.Hash(model.Password)
};
await UserContext.AddAsync(user);
await UserContext.SaveChangesAsync();
}
}
//Startup.cs in Server project
public void ConfigureServices(IServiceCollection services)
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UsersConnection"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
}));
在发布项目时,我为 'users' 配置了一个 Azure SQL 数据库,选中复选框以在运行时使用 UsersConnection 字符串,并在发布时应用 UserContext Entity Framework 迁移.
我用的是visual studio2019,目标框架是netcoreapp3.1。我将不胜感激任何指导。谢谢!
编辑
看了详细的日志,好像连数据库都没建好?
INSERT INTO [Users] ([Email], [Password])
VALUES (@p0, @p1);
2020-06-22 22:19:47.208 +00:00 [Error] Microsoft.EntityFrameworkCore.Update: An exception occurred in the database while saving changes for context type 'BlazorTodos.Server.Data.UserContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Users'.
感谢杰森的所有建议。帮助我朝着正确的方向前进。
这个问题很愚蠢。显然,数据库和 Entity Framework 设置已自动为我的“用户”和“Todos”数据库提供相同的字符串:
Data Source=tcp:myserver.database.windows.net,1433;Initial Catalog=todos;User Id=<myid>@myserver;Password=<my-password>
但我没有注意到它是同一个字符串。我只需要将 Initial Catalog
更改为 users
并使用编辑后的字符串再次检查“在运行时使用此连接字符串”和“在发布时应用此迁移”框。那解决了问题。
我一直在尝试在 Azure 应用服务上部署我的 asp.net 核心托管 blazor webassembly 应用程序,但是,我无法让 api 工作。当我尝试将用户数据保存在数据库中时,出现 400 错误请求错误。它在本地主机上运行良好。我环顾四周,发现建议我使用 Azure 中的日志流来获取更详细的错误消息,尽管我不确定这些细节是否真的有帮助,但还是在这里。
2020-06-22 22:24:54 MYPROJECT POST /api/Register/Post X-ARR-LOG-ID=ef27263e-dead-417a-b136-89a217a6f931 443 - MYIP Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/83.0.4103.97+Safari/537.36 ARRAffinity=7f74b113b9ae575a087ae1fa07a63858e6e34f27224b7aa1a957b06074e65ffd https://myproject.azurewebsites.net/Register
相关申请代码如下:
//Register.razor in client project
@code {
private RegisterModel Model = new RegisterModel();
private bool ShowErrors;
private List<string> Errors = new List<string>();
private async Task HandleValidSubmit()
{
ShowErrors = false;
Errors.Clear();
if (Model.Password.Length >= 6 && Model.Password == Model.ConfirmPassword)
{
await HttpClient.PostAsJsonAsync<RegisterModel>("api/Register/Post", Model);
NavigationManager.NavigateTo("/");
}
else
{
if (Model.Password.Length > 100 || Model.Password.Length < 6)
{
Errors.Add("Password must be between 6 and 100 characters in length.");
}
if (Model.Password != Model.ConfirmPassword)
{
Errors.Add("Passwords do not match.");
}
ShowErrors = true;
}
}
}
//RegisterController.cs in server project
[Route("api/Register")]
public class RegisterController : Controller
{
private UserContext UserContext { get; set; }
private IHasher Hasher = new Pbkdf2Hasher();
public RegisterController (UserContext userContext)
{
UserContext = userContext;
}
[RequireHttps]
[HttpPost]
[Route("Post")]
public async Task Post([FromBody]RegisterModel model)
{
var user = new UserModel
{
Email = model.Email,
Password = Hasher.Hash(model.Password)
};
await UserContext.AddAsync(user);
await UserContext.SaveChangesAsync();
}
}
//Startup.cs in Server project
public void ConfigureServices(IServiceCollection services)
services.AddDbContext<UserContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("UsersConnection"),
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.EnableRetryOnFailure();
}));
在发布项目时,我为 'users' 配置了一个 Azure SQL 数据库,选中复选框以在运行时使用 UsersConnection 字符串,并在发布时应用 UserContext Entity Framework 迁移.
我用的是visual studio2019,目标框架是netcoreapp3.1。我将不胜感激任何指导。谢谢!
编辑
看了详细的日志,好像连数据库都没建好?
INSERT INTO [Users] ([Email], [Password])
VALUES (@p0, @p1);
2020-06-22 22:19:47.208 +00:00 [Error] Microsoft.EntityFrameworkCore.Update: An exception occurred in the database while saving changes for context type 'BlazorTodos.Server.Data.UserContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'Users'.
感谢杰森的所有建议。帮助我朝着正确的方向前进。
这个问题很愚蠢。显然,数据库和 Entity Framework 设置已自动为我的“用户”和“Todos”数据库提供相同的字符串:
Data Source=tcp:myserver.database.windows.net,1433;Initial Catalog=todos;User Id=<myid>@myserver;Password=<my-password>
但我没有注意到它是同一个字符串。我只需要将 Initial Catalog
更改为 users
并使用编辑后的字符串再次检查“在运行时使用此连接字符串”和“在发布时应用此迁移”框。那解决了问题。