我在WebApi项目中使用.Net框架4.6.1和Swashbuckle版本5.3.2。Swagger UI没有提供将输入作为请求正文发送到使用自定义模型活页夹的POST Api的选项。
-使用型号:
[ModelBinder(typeof(FieldValueModelBinder))]
public class Employee
{
public int EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string City { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
-使用的API Post方法:
[HttpPost]
// POST: api/Employee
public HttpResponseMessage Post([ModelBinder(typeof(FieldValueModelBinder))]Employee emp)
{
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.BadRequest, "Please provide valid input");
else
//Add Employee logic here
return Request.CreateResponse(HttpStatusCode.OK, "Employee added sucessfully");
}
Run Code Online (Sandbox Code Playgroud)
-所用的粘合剂型号:
public class FieldValueModelBinder : System.Web.Http.ModelBinding.IModelBinder
{
/// <summary>
/// Store received data in API in KeyValuePair
/// </summary> …Run Code Online (Sandbox Code Playgroud)