ASP.NET MVC 4 RC的Web API参数绑定问题 [英] ASP.NET MVC 4 RC Web API Parameter Binding Issue

查看:194
本文介绍了ASP.NET MVC 4 RC的Web API参数绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了我认为是ASP.NET MVC 4 RC的Web API中的一个奇怪的结合问题。我打算从客户端接受交请求的方法。问题是,没有一个参数的时候,POST方法被调用时,我让我的破发点上罚球线和姓名,电子邮件都是空都具有约束力。如果我更改请求的类型,在JavaScript的机会很少,那么下面的获取函数调用绑定的参数。

I'm getting what I consider to be a weird binding issue in the ASP.NET MVC 4 RC Web API. I have a method intended to accept post requests from the client. The issue is that none of the parameters are binding when the post method is called, I get to my break point on the throw line and name, email are both null. If I change the type of the request to GET in the JavaScript, then the Get function below is called with parameters bound.

为什么参数没有绑定的邮政法,以及如何解决这个问题?

Why are the parameters failing to bind for the Post method, and how can I fix this?

send: function(evt) {
    evt.preventDefault();
    $.ajax( {
        url: '/api/person',
        data: this.model.toJSON(),
        type: "POST",
        dataType: "json",
        success: function(data) {
            console.log("Success");
        },
        error: function(data) {
            console.log("Error");
        }
    });
     }

以下是控制器动作:

The following is the controller actions:

public void Get(string name, string email) {
    throw new NotImplementedException();
}

public void Post(string name, string email) {
    throw new NotImplementedException();
}

注:

  • 在我使用所有的默认ASP.NET MVC 4 RC网络API(所以解串器应该是Json.NET)
  • 在JS调试器的Chrome网络标签显示帖子在表单数据的参数正确。

推荐答案

与MVC(网页),简单的参数类型的<一个href="http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx">will不,默认情况下,从后车身,而是从URI 绑定。所以,你的code作为,是你将需要通过和电子邮件参数在查询中的名字符串或路由参数。

Unlike MVC (web pages), simple parameters types will not, by default, bind from the post body but instead from the URI. So, with your code as-is you would need to pass the name and email parameters in the query string or as route parameters.

然而,这可以容易地通过创建一个模型类型(MVC中白话),并使用该方法参数求解。事实上,你可以使用它为(在你给的情况下),如果再使用 [FromUri] 的get方法:

However this can easily be solved by creating a model type (in MVC vernacular) and using that for the method parameters. In fact you can then use it for both (in the case that you've given), if you then use [FromUri] on the get method:

public class SomeParams {
  public string name { get; set; }
  public string email { get; set; }
}

//now an alternative way to write the Get method
public MyResult Get([FromUri] SomeParams p){
  //members are bound from the query string (more like MVC traditional binding)
  //note - as in MVC, SomeParams will need a default constructor for this to work.
}

public PostResult Post(SomeParams p){
  //'p' is bound from your JSON (assuming correct format)
  //because 'complex' types are deserialized using formatters
  //only one object can be read from the body with a formatter in Web API
  //as the request body is not buffered; unlike MVC.
}

我已经陷在了方法的返回类型,只是因为他们需要返回的东西!

我真的不建议通过麦克失速的文章看完,我链接到上面(他的许多其他人)。

I really do recommend reading through Mike Stall's article that I link to above (and many of his others).

这是很有诱惑力的假定Web API,因为它共享相同的模式和MVC甚至类的名字,其实是一样的MVC - 但事实并非如此。我不知道最初为什么会是这种情况(因为我已经对自己的MVC的顶部写了很多REST服务,并发现它是pretty的织补冷静,一​​旦你已经写了一些实用工具类和基础类增强),但他们已经采取了适当的样子在写一个web API的挑战,我想,也许是对已经取得的办法,他们有变化。

It's tempting to assume that the Web API, because it shares the same paradigms and even class names of MVC, is actually the same as MVC - but it's not. I wondered initially why this was the case (as I've written a lot of REST services on top of MVC myself and found it to be pretty darn cool, once you've written a few utility classes and base-class enhancements), but they've taken a proper look at the challenges of writing a web API and I think are probably right to have made the changes in approach that they have.

然而,这并不意味着我们要采取一些事情,现在我们可能认为是理所当然和重新学习他们对Web API。

However, it does mean that we have to take a few things we might now take for granted and re-learn them for the Web API.

这篇关于ASP.NET MVC 4 RC的Web API参数绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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