本文介绍了服务器端Blazor页面发布到Razor页面http错误400的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将用户名和密码从Blazor页面(.razor)发布到Razor页面(.cshtml),但我始终遇到HTTP错误400.

I am trying to post my username and password from a Blazor page (.razor) to a Razor Page(.cshtml) but I get http error 400 all the time.

LoginControl.razor

<form method="post" action="login">
        <input type="text"
               name="username"
               @bind="@Username"
               placeholder="User Name" />
        &nbsp;&nbsp;
        <input type="password"
               name="password"
               placeholder="Password"
               @bind="@Password" />&nbsp;&nbsp;&nbsp;
        <button class="ml-md-auto btn btn-primary">Loggin</button>
    </form>

Login.cshtml.cs

public async Task<IActionResult> OnPostAsync([FromServices] IUserProvider provider, string username, string password)
    {
Login stuff
}

推荐答案

错误请求(400)表示服务器无法理解您

Bad request (400) means that the server cannot understand you

method ="post"不适用于Blazor或任何其他SPA框架.在Blazor中,有防止回发的特殊代码.当然,您可以强制执行回发,这意味着您的Blazor页面已过期,并且如果您尝试返回到该页面,则将其重新呈现,并且您之前的所有状态都将丢失.

The method="post" is not applicable in Blazor or any other SPA frameworks. In Blazor there is special code that prevent a post back. Of course you can force a postback, which means that your Blazor page expires, and if you try to return to it, it is rerendered, and all your previous state is lost.

您要在此处执行的操作是在Blazor组件中获取用户的凭据,然后尝试将数据发布到Razor页面模型进行验证.您不能那样做,也不应该那样做.您应该将用户重定向到登录页面,在该页面中他输入自己的凭据,并且应该针对某种存储库(例如SqlServer数据库)对这些凭据进行验证,然后返回到Blazor应用程序.请记住,重定向到剃刀页面"意味着您的Blazor应用程序已过期.再次,您不将用户的凭据发布到登录剃须刀"页面.用户应在Login.cshtml

What you're trying to do here is to get the user's credentials in a Blazor component, and then try to post the data to a Razor Page Model for validation. You can't do that, and you should not do that. You should redirect your user to a login page in which he enters his credentials and those credentials should be validated against some sort of store, say SqlServer database, and then returns to the Blazor app. Remember, the redirection to a Razor Page means that your Blazor app has expired. One again you do not post the user's credentials to the Login Razor Page. The user should enter his his credentials on Login.cshtml

我建议您使用Blazor内置系统来启用身份验证和授权.该系统使用身份Ui(剃刀页)来验证用户.它正是您想要做的. 在此处查看... .另外,请使用Blazor提供的Forms Components,例如EditForm,InutText等.

I'd suggest you to use the Blazor built-in system to enable authentication and authorization. This system uses the Identity Ui (Razor Pages) to authenticate the user. It does exactly what you're trying to do. See here.... Also, use the Forms Components provided by Blazor, such as EditForm, InutText, etc.

这篇关于服务器端Blazor页面发布到Razor页面http错误400的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 02:40