无法将JSON发布到ASP.NET Core RazorPage处理程序 [英] Cannot post JSON to an ASP.NET Core RazorPage handler

查看:118
本文介绍了无法将JSON发布到ASP.NET Core RazorPage处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core RazorPage作为MVC控制器的替代产品,并且我希望能够通过XMLHttpRequest提交客户端表单.我已经弄清楚了XSRF令牌位,可以通过集合,但是RazorPages框架似乎并没有按预期处理入站JSON有效负载并将其绑定到属性.

一些代码:

页面的模型(.cshtml.cs):

 公共类IndexModel:PageModel{私有数据库数据库;私有ILogger记录器;[BindProperty]公共AddRequestModel MyRequest {get;放;}公用IndexModel(数据库数据库,ILogger< IndexModel>记录器){this.database =数据库;this.logger = logger;}公共无效的OnGet(){}公共IActionResult OnPostValidate(){如果(ModelState.IsValid){//...}返回新的BadRequestObjectResult(ModelState);}公共异步Task< IActionResult>OnPutConfirmAsync(){//...}} 

和客户端帖子:

  const url =?handler = validate";const data = {MyRequest:this.fields};等待axios.post(URL,data); 

我已确认数据提交正确:

在提交请求之前,axios正在添加X-XSRF-TOKEN标头.服务器以错误列表响应的事实表明,它不是导致问题的XSRF令牌:

请注意,MyRequest对象不包含请求有效载荷中的值-未按预期进行绑定(否则,FirstName将不会返回所需的错误).如何告诉RazorPages接受JSON请求并将其绑定到我的媒体资源?

解决方案

我能够通过添加 FromBody 来获得Binding的工作,类似于ASP.NET Web API 2的工作方式.

>

  [BindProperty,FromBody]公共BroadcastMessageEditingViewModel BindingInfo {get;放;} 

I'm working with an ASP.NET Core RazorPage as an alternative to an MVC controller, and I want to be able to submit the client side form via XMLHttpRequest. I've already figured out the XSRF token bits so that passes the muster, but the RazorPages framework doesn't seem to process the inbound JSON payload and bind it to the property as expected.

Some code:

The page's model (.cshtml.cs):

public class IndexModel : PageModel
{
    private Database database;
    private ILogger logger;

    [BindProperty]
    public AddRequestModel MyRequest { get; set; }

    public IndexModel(Database database, ILogger<IndexModel> logger)
    {
        this.database = database;
        this.logger = logger;
    }

    public void OnGet() {}

    public IActionResult OnPostValidate()
    {
        if (ModelState.IsValid)
        {
            // ...
        }

        return new BadRequestObjectResult(ModelState);
    }

    public async Task<IActionResult> OnPutConfirmAsync()
    {
        // ...
    }
}

And the client side post:

const url = "?handler=validate";
const data = { MyRequest: this.fields };
await axios.post(url, data);

I have verified the data is being submitted correctly:

That X-XSRF-TOKEN header is being added by axios before the request is submitted. The fact that the server responds with a list of errors indicates that it's not the XSRF token causing the problem:

Note the MyRequest object does not contain the values from the request payload - it was not bound as expected (FirstName would not return a required error otherwise). How can I tell RazorPages to accept the JSON request and bind it to my property?

解决方案

I was able to get the Binding works by adding FromBody similar to how it worked for ASP.NET Web API 2.

    [BindProperty, FromBody]
    public BroadcastMessageEditingViewModel BindingInfo { get; set; }

这篇关于无法将JSON发布到ASP.NET Core RazorPage处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆