首页 文章

从web-api返回到Swagger-ui的位置响应头

提问于
浏览
3

我正在返回一个Uri作为我的web-api控制器响应的位置 Headers ,如下所示:

[HttpPost]
public HttpResponseMessage PostTenant([FromBody] JObject value)
{
    string tenantName;
    try
    {
        tenantName = value.GetValue("name").ToString();
    }
    catch (Exception e)
    {
        throw new HttpResponseException(
            this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, e));
    }

    try
    {
        DbInteract.InsertTenant(tenantName.Replace(" ", string.Empty));
        var uri = Url.Link("TenantHttpRoute2", new { tenantName });
        var response = new HttpResponseMessage(HttpStatusCode.Created);
        response.Headers.Location = new Uri(uri);
        return response;
    }
    catch...
}

swagger-ui客户端发出 POST 请求,但响应头不包含 Location uri . 它看起来像这样:

POST request from Swagger-ui

如图所示, RESPONSE HEADERS 是{"content-type":null}


这篇帖子请求的招摇JSON是:

"post": {
            "tags": [
                "Tenant"
            ],
            "summary": "Add a new tenant",
            "description": "Adds a tenant and returns admin user account information to be used in further calls.",
            "consumes": [
                "application/json",
                "application/xml"
            ],
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "in": "body",
                    "name": "Tenant",
                    "description": "Name of the tenant must be alphanumeric without any special characters.",
                    "required": true,
                    "schema": {
                        "$ref": "#/definitions/CreateTenant"
                    }
                }
            ],
            "responses": {
                "201": {
                    "description": "Tenant Inserted",
                    "headers": [
                        {
                            "description": "Location",
                            "type": "string"
                        }
                    ]
                },
                "400": {
                    "description": "Invalid JSON Format of input"
                },
                "405": {
                    "description": "Tenant by this name already exists; Use PUT instead"
                },
                "500": {
                    "description": "InternalServerError"
                }
            }
        }

这有什么不对?为什么我不能在swagger-ui中看到响应头?如果您需要更多信息,请与我们联系 . 谢谢

2 回答

  • 0

    意图似乎很清楚:成功的响应返回201 Created,其Location标头指向新创建的资源 . 应该管用 ...

    我可能会删除“produce”属性,因为您没有在任何地方定义任何响应 .

  • 0

    试试:

    "headers" : {  "Location" : { "description": "...", "type" : "string" } }
    

    代替:

    "headers" : []
    

    所以对象而不是数组 .

相关问题