小编Nid*_*nar的帖子

自定义模型活页夹未从Swagger UI调用

我在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)

asp.net-web-api swagger swashbuckle

5
推荐指数
1
解决办法
1142
查看次数

标签 统计

asp.net-web-api ×1

swagger ×1

swashbuckle ×1