2017-05-15 2 views
0

アクションフィルタ属性を例外は、「HTTPヘッダが送信された後にリダイレクトすることができません」例外。あなたがこれを行うことはできませんリダイレクト

+1

あなたは[OnActionExecuting](https://msdn.microsoft.com/en-us/library/system.web.http.filters.actionfilterattributeオーバーライドする必要があります:

あなたはこのような何かをしたいです。 onactionexecuting(v = 118).aspx) – mmushtaq

+0

これを実装する方法は? @mmushtaq –

+0

メソッド名を 'OnResultExecuted'と' OnResultExecuting'に変更するだけです。このメソッドはpublic override void OnActionExecuting(HttpActionContext actionContext){....} 'のようにします。 – mmushtaq

答えて

0

HTTPヘッダーが

を送ってきたので、あなたは(Response.IsRequestBeingRedirectedプロパティを確認できた後

リダイレクトが試みられたとき、MSDN Documentationによると、Response.Redirect("url")はHttpExeptionをスローしますブール)を呼び出す前に。

のように:あなたがリダイレクトする必要はありません

// Causes headers to be sent to the client (Http "Location" response header) 
Response.Redirect("http://www.stackoverflow.com"); 
if (!Response.IsRequestBeingRedirected) 
// Will not be called 
Response.Redirect("URL"); 
+0

私はこれを試してみましたが、それは私の機能に応じて私はこれを使用することができますエラーを私に与える@Francois –

+0

あなたはエラーを提供することはできますか? –

+0

ログインにリダイレクトされません。 "HTTPヘッダーが送信された後にリダイレクトできません" –

0

ActionExecutingContextの結果を設定して、行きたい場所に誘導します。

public class CustomSecurityAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (tokenContainer.ApiToken != null) return; 

     //this will re-route you action 
     filterContext.Result = new ViewResult 
     { 
      ViewName = "Login", //whatever view you are routing to 
      ViewData = new ViewDataDictionary<HandleErrorInfo>(model), //you can leave it blank if your view doesn't need data 
      TempData = filterContext.Controller.TempData 
     }; 
     filterContext.ExceptionHandled = true; 
     filterContext.HttpContext.Response.Clear(); 
     filterContext.HttpContext.Response.StatusCode = 403; 
     filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; 
    } 
}