2015-11-28 32 views
2

我返回一个URI作为从我的网络API控制器响应的位置标头,如下图所示:Location响应头从网上API返回到扬鞭的UI

[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... 
} 

招摇的用户界面客户端使POST请求,但响应标题不包含位置 uri。这似乎是这样的:

POST request from Swagger-ui

正如你可以在图片中看到,该响应头是 { “内涵式”:空 }


招摇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中看不到响应头?如果您需要更多信息,请告诉我。谢谢

回答

0

意图似乎足够清楚:成功的响应返回201创建一个位置标题指向新创建的资源。应该工作...

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

0

尝试用:

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

相反的:

"headers" : [] 

所以对象,而不是阵列。

+0

要改善你的答案,也许你可以格式化源代码 – Kalamarico