[英].NET Core API not working after deploying on Azure
I have a simple .net core web api with angular app on the client side.我有一个简单的 .net 核心 web api 在客户端上带有 ZD18B8624A0F5F7221DA7B82365FC5。
When I call my api locally ( http://localhost:5000/api/auth/register ) it works without any problems.当我在本地( http://localhost:5000/api/auth/register )调用我的 api 时,它可以正常工作。 After deploying app to azure and calling it ( https://myapp.azurewebsites.net/api/auth/register ), I get a 400 Bad Request error without any message.
将应用程序部署到 azure 并调用它( https://myapp.azurewebsites.net/api/auth/register )后,我收到 400 Bad Request 错误,没有任何消息。
I was trying to configure app service and sql server on azure portal but I'm not sure what to change.我试图在 azure 门户上配置应用程序服务和 sql 服务器,但我不确定要更改什么。
AuthController.cs AuthController.cs
[HttpPost("register")]
public async Task<IActionResult> Register(UserToRegisterDto userToRegister)
{
if (await AuthService.UserExists(userToRegister.UserName))
{
return BadRequest("Username already exists");
}
var userToCreate = new User() { UserName = userToRegister.UserName };
await AuthService.Register(userToCreate, userToRegister.Password);
// TODO: Change to return CreatedAtRoute
return StatusCode(201);
}
AuthService.cs身份验证服务.cs
public async Task<User> Register(User user, string password)
{
GenerateHashedPassword(user, password);
await usersRepository.Create(user);
return user;
}
auth.service.ts auth.service.ts
private baseUrl = environment.apiUrl + 'auth/';
public register(userToRegister: UserToRegister) {
return this.http.post(this.baseUrl + 'register', userToRegister);
}
environment.ts环境.ts
export const environment = {
production: false,
apiUrl: 'http://localhost:5000/api/'
};
environment.prod.ts环境.prod.ts
export const environment = {
production: true,
apiUrl: 'api/'
};
First check your log in your azure web app there would be log when you accessing your web app首先在您的 azure web 应用程序中检查您的日志,当您访问 web 应用程序时会出现日志
Second check exception capture in your azure web app any exeception occur will be capture with status code and the detail您的 azure web 应用程序中的第二次检查异常捕获将使用状态码和详细信息进行捕获
Finally try to change your api url like this.最后尝试像这样更改您的 api url。 But I dont think the error come from this setting
但我不认为错误来自这个设置
export const environment = {
production: true,
apiUrl: 'https://myapp.azurewebsites.net/api/'
};
Update to auto run migrate when you deploy new version in azure web app you can add these code snippet当您在 azure web 应用程序中部署新版本时更新为自动运行迁移,您可以添加这些代码片段
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(...);
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
UpdateDatabase(app);
...
}
private static void UpdateDatabase(IApplicationBuilder app)
{
using (var serviceScope = app.ApplicationServices
.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<MyDbContext>())
{
context.Database.Migrate();
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.