【发布时间】:2018-02-27 11:39:36
【问题描述】:
我的 Html 代码是这样的。
<input type="text" name="Id" value="123"/>
<input type="text" name="Name" value="XYZ"/>
<input type="text" name="Abc.Address" value="ABC,XYZ 700567"/>
<input type="text" name="Abc.Mobile" value="6958743216"/>
类:-
class A
{
public string Id { get; set; }
public string Name { get; set; }
public Abc Abc { get; set; }
}
class Abc
{
public string Address { get; set; }
public string Mobile{ get; set; }
}
通过上部输入当我调用 MVC 控制器时,会发生绑定。 以下方法的手段
public ActionResult Post(A x)
x 中的值是
x.Id="123"
x.Name="XYZ"
x.Abc.Address="ABC,XYZ 700567"
x.Abc.Mobile="6958743216"
但是当我在 web api 中尝试相同的事情时,它给了我 415(不支持的媒体类型)。以下方法的手段
public IHttpActionResult Post(A x)
它不是绑定,而是给出 415 状态代码,表示不支持的媒体类型。
我知道问这个问题很愚蠢,但为什么会发生这种情况?如果我想做与 mvc 中发生的相同的绑定,我该如何实现?(我不想做这样的事情
--> public IHttpActionResult Post([SomeBinder]A x))
有没有办法做到这一点?
【问题讨论】:
-
你是如何发布数据的?
-
_您如何发布数据? _ 不明白。如果您的意思是我如何发送这些数据,那么 ans 是通过表单提交
-
Web API 不应该直接用于 HTML 表单,这就是 MVC 的用途
-
那么如果我想做api,那我该怎么做呢?有什么解决办法吗?
-
你是从 fiddler 还是 postman 调用这个动作方法?
标签: c# .net asp.net-mvc asp.net-web-api