c#HTTP Post IHttpActionResult
我有两个httppost方法,一个是使用一个具体的类,工作正常。另一个是接收接口。当我使用postman时,接口方法得到null。我是否能够将模型类发送到接口?我是否需要使用自定义模型绑定器?如果需要,我需要在哪里添加它c#HTTP Post IHttpActionResult,c#,asp.net,C#,Asp.net,我有两个httppost方法,一个是使用一个具体的类,工作正常。另一个是接收接口。当我使用postman时,接口方法得到null。我是否能够将模型类发送到接口?我是否需要使用自定义模型绑定器?如果需要,我需要在哪里添加它 public IHttpActionResult InsertData([FromBody] DataLog _datalog) public IHttpActionResult Insert([ModelBinder(typeof(IDataLog))] IDataLog
public IHttpActionResult InsertData([FromBody] DataLog _datalog)
public IHttpActionResult Insert([ModelBinder(typeof(IDataLog))] IDataLog _log)
我想这也能回答你的问题 接口 不能将接口用作对象的原因是因为它们没有公共访问器。它们也不能有字段。它只是属性和方法的签名 接口
我不知道我是否理解你的问题。无论如何,关于代码,我必须说:
public ActionResult Index()
{
return View();
}
public IHttpActionResult InsertData([FromBody] DataLog _datalog)
这样做:
public ViewResult Index()
{
return View();
}
[HttpPost]
public IHttpActionResult InsertData([FromBody] DataLog _datalog)
public ActionResult Index()
{
return View();
}
public IHttpActionResult InsertData([FromBody] DataLog _datalog)
让代码像这样编译得更快,添加HttpPost属性,即使知道这是一个POST操作:
这样做:
public ViewResult Index()
{
return View();
}
[HttpPost]
public IHttpActionResult InsertData([FromBody] DataLog _datalog)
希望这些好的实践能对你有所帮助!:) 关于接口和类之间差异的良好信息。你就快到了。您需要创建自定义模型绑定器,并将其与
ModelBinder
属性一起使用。因此[ModelBinder(typeof(DataLogModelBinder))]
查看本文以获得编写自定义模型绑定器的帮助:@KevinBabcock这是model@Jefferson对不起,我误读了你的评论。它将是一个独立于模型的类。定义它的位置取决于应用程序的组织和结构。谢谢@jawad,那么ModelBinder的用途是什么?ModelBinder属性指定了应该用于绑定类的动作参数的IModelBinder的类型是否有其他方法可以这样做?而不是复制粘贴有关如何创建绑定器的信息,将其用于类,然后在您的API调用中实现该类,关于点[1]-当使用依赖项注入时,我总是看到\uu
。e、 g,私有只读IDataContext\u dataContext代码>。它具体是说在哪里不使用那个符号?@SeanT遵循这一点:要理解这一点,它没有提到使用前导下划线,这是指无符号整数。这与下划线无关。使用下划线很好,我能理解避免使用关键字之类的东西。