简体   繁体   中英

.NET Core API not working after deploying on Azure

I have a simple .net core web api with angular app on the client side.

When I call my api locally ( http://localhost:5000/api/auth/register ) it works without any problems. 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.

I was trying to configure app service and sql server on azure portal but I'm not sure what to change.

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

        public async Task<User> Register(User user, string password)
        {
            GenerateHashedPassword(user, password);

            await usersRepository.Create(user);

            return user;
        }

auth.service.ts

  private baseUrl = environment.apiUrl + 'auth/';

  public register(userToRegister: UserToRegister) {
    return this.http.post(this.baseUrl + 'register', userToRegister);
  }

environment.ts

export const environment = {
  production: false,
  apiUrl: 'http://localhost:5000/api/'
};

environment.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

Second check exception capture in your azure web app any exeception occur will be capture with status code and the detail

Finally try to change your api url like this. 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

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();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM