簡體   English   中英

Azure ASP.NET 核心 POST 400 錯誤請求 Blazor Webassembly

[英]Azure ASP.NET Core POST 400 bad request Blazor Webassembly

I have been trying to deploy my asp.net core hosted blazor webassembly app on Azure App Service, however, I am having trouble getting the api to work. 當我嘗試將用戶的數據保存在數據庫中時,我收到 400 bad request 錯誤。 它在本地主機上運行良好。 我環顧四周,發現建議我在 Azure 中使用 Log Stream 以獲得更詳細的錯誤消息,盡管我不確定這些細節是否真的有幫助,但這里就是這樣。

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

在發布項目時,我為“用戶”配置了 Azure SQL 數據庫,選中復選框以在運行時使用 UsersConnection 字符串,並在發布時應用 UserContext 實體框架遷移。

我用的是visual studio 2019,目標框架是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'.

感謝所有的建議,傑森。 幫助我朝着正確的方向前進。

這個問題很愚蠢。 顯然,數據庫和實體框架設置自動為我的“用戶”和“待辦事項”數據庫提供了相同的字符串:

Data Source=tcp:myserver.database.windows.net,1433;Initial Catalog=todos;User Id=<myid>@myserver;Password=<my-password>

但我沒有注意到它是同一個字符串。 我只需將Initial Catalog更改為users ,並使用編輯后的字符串再次檢查“在運行時使用此連接字符串”和“在發布時應用此遷移”框。 這解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM