2017-07-26 88 views
0

我有一個OwinMiddlewareInvoke方法尋找一種像這樣:找不到IOwinContext對象內部的獲取方法的結果

public override async Task Invoke(IOwinContext context) 
{ 
    ... 
    //The next line launches the execution of the Get method of a controller 
    await Next.Invoke(context); 
    //Now context.Response should contain "myvalue" right? 
    ... 
} 

Invoke方法調用Get方法,位於控制器內部,看起來有點像這樣:

[HttpGet] 
public IHttpActionResult Get(some params...) 
{ 
    ... 
    return "myvalue"; 
    ... 
} 

Get方法執行後,該程序可以追溯到我的中間件Invoke方法。我認爲Get方法的響應,即myvalue應該包含在context.Response之內,但我不知道在哪裏,因爲它充滿了所有的東西。

+0

這將是身體流。這似乎是[XY問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。你試圖達到的最終目標是什麼? – Nkosi

+0

我想從Invoke方法中訪問「myvalue」以將其插入到一個arraylist中 – nix86

回答

0

Actualy響應流中,你需要做到這一點得到響應回來一部開拓創新的形式

try{ 
     var stream = context.Response.Body; 
     var buffer = new MemoryStream(); 
     context.Response.Body = buffer; 
     await _next.Invoke(environment); 
     buffer.Seek(0, SeekOrigin.Begin); 
     var reader = new StreamReader(buffer); 
     // Here you will get you response body like this 
     string responseBody = reader.ReadToEndAsync().Result; 
     // Then you again need to set the position to 0 for other layers 
     context.Response.Body.Position = 0; 
     buffer.Seek(0, SeekOrigin.Begin); 
     await buffer.CopyToAsync(stream); 
    } 
    catch(Exception ex) 
    { 

    } 
+0

這是準確的,您應該更新它以匹配OPs用例,並嘗試解釋您爲什麼執行了您的建議。 – Nkosi

+0

是的字符串responseBody正是我所期待的。 – nix86