【发布时间】:2021-08-11 12:50:01
【问题描述】:
我用的是asp dotnet core V6(即预览版)。
我有两种相同的方法,一种在 MVC 控制器中,另一种在 Api 控制器中。方法的签名是:
对于MVC
public async Task<ActionResult> Variable(Models.DTParameters dtp)
用于api
[HttpGet]
[Consumes("application/x-www-form-urlencoded")]
[Route("api/Select/Variable")]
public async Task<ActionResult> Variable([FromQuery] Models.DTParameters dtp)
方法被 jquery $.ajax调用
对于 MVC:
{
method: 'POST',
url: '/Select/Variable'
}
对于 api:
{
method: 'GET',
url: '/api/Select/Variable'
}
| MVC | api |
|---|---|
| All is fine | Only the simple property (draw, start, length) are binded.the others (search...) are null. |
If I try with method='POST' and [FromBody], I end with an error: "Not supported media type" |
关于如何让 ApiController 正确绑定 DTParameters 的任何想法?
----- 编辑 1
如果我从我的 api 控制器中删除 [ApiController] 属性,它会起作用:
//[ApiController]
public class SelectController : ControllerBase
对于我的 mvc 控制器,声明是:
public class SelectController : Controller
----- 编辑 2
带有 [ApiController] 和以下声明:
[HttpPost]
[Consumes("application/json")]
[Route("api/Select/Variable")]
public async Task<ActionResult> Variable(Models.DTParameters dtp)
我现在收到错误 400:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"出现一个或多个验证错误。","status":400, "traceId":"00-9753afa4b5fc5ed24f3605c3edd51d8e-25872b0ff804e179-00","errors":{"$.order[0].dir":["JSON 值无法转换为adapator.wgui.Models.DTOrderDir。路径: $.order[0].dir | LineNumber: 0 | BytePositionInLine: 151."]}}
其中位置值 151 对应于最后一个 c in 之后的 "
"order":[{"column":0,"dir":"asc"}]
似乎活页夹无法处理 DTOrderDir 枚举。
----- 附件
DTParmeters 在哪里:
public class DTParameters {
public int Draw { get; set; }
public DTColumn[] Columns { get; set; }
public DTOrder[] Order { get; set; }
public int Start { get; set; }
public int Length { get; set; }
public DTSearch Search { get; set; }
public DTKeyValue[] MoreForSearch { get; set; }
}
public class DTKeyValue {
public string Key { get; set; }
public string Value { get; set; }
}
public class DTColumn {
public string Data { get; set; }
public string Name { get; set; }
public bool Searchable { get; set; }
public bool Orderable { get; set; }
public DTSearch Search { get; set; }
}
public class DTOrder {
public int Column { get; set; }
public DTOrderDir Dir { get; set; }
}
public enum DTOrderDir {
asc,
desc
}
public class DTSearch {
public string Value { get; set; }
public bool Regex { get; set; }
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-core asp.net-web-api .net-core