2017-04-08 218 views
0

我想在登錄後修改我的HeaderResponse函數。其實我可以修改/添加新的部分在這個響應字段我想修改所有xhr請求的頭部

HttpContext.Response.AddHeader(「Example」,「example」);

但它並不影響所有請求。它隻影響一個請求。

[HttpPost] 
     [Route("Account/Login")] 
     [ValidateAntiForgeryToken] 

     public JsonResult Login(string username, string password) 
     { 
      AccountModel am = new AccountModel(username, password); 
      am.Find(username); 
      if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || 
       am.Login(username, password) == null) 
      { 

       var data = new { status = "false", message = "Account is Invalid" }; 

       return Json(data,JsonRequestBehavior.AllowGet); 
      } 

      ////////HttpContext.Response.AddHeader("Example", "example"); 

      return Json(....,JsonRequestBehavior.AllowGet); 
     } 

基本上,我想在登錄進度後創建HeaderResponse。我想,我需要一種HttpGlobalContext.Response或類似的東西。有可能這樣做嗎?或者有沒有其他辦法。

+0

您使用owin標頭值? –

+0

是的,即時通訊使用owin。 – Cracked

回答

0

有多種方法可以實現您嘗試實現的目標。

1.Adding一個OWIN中間件

public override async Task Invoke(IOwinContext context) { 
    //call Headers.Add() method 
    context.Response.Headers.Add("key",value); 
    await Next.Invoke(context); } 

2.Adding鹼控制器重寫OnActionExecuting添加