简体   繁体   English

.NET Core API 在 Azure 上部署后无法正常工作

[英].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.

相关问题 API 连接字符串在部署到 Azure 后不起作用 - API Connection String Not Working After Deploying to Azure .net 核心 API 文件上传不适用于 Azure - .net core API fileupload not working on Azure 部署到Azure后OAuth无法正常工作[ASP.NET MVC] - OAuth not working after deploying to Azure [ASP.NET MVC] 部署到 Azure 后 DocumentClient 不起作用 - DocumentClient not working after deploying to Azure .NET Core 3.1 web API 在本地构建良好,但通过 Z3A580F1420psO3psO3973F13ZB0 Dev 部署时出错 - .NET Core 3.1 web API builds fine locally, but gets errors when deploying via Azure DevOps HTTP 错误 500.32 - 将自包含 .Net Core 3.1 应用程序部署到 Azure 后,ANCM 无法加载 dll - HTTP Error 500.32 - ANCM Failed to Load dll after deploying a Self Contained .Net Core 3.1 App to Azure 将 .NET Core Web API 部署到 IIS 服务器 - Deploying .NET Core Web API to IIS Server 将 .Net Core API 部署并运行到 FTP 服务器 - Deploying and running .Net Core API to an FTP Server 从 .NET 核心 2.2 迁移到 .NET 5 后,应用程序在部署到 azure 应用程序服务后无法启动 - 启动超时 - After migration from .NET core 2.2 to .NET 5 the application won't start after deploying to azure app service - startup timeout 部署 .net core api AWS 无服务器应用程序后出现 403 Forbidden/500 内部服务器错误 - 403 Forbidden/500 Internal Server Error after deploying .net core api AWS Serverless application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM