2010-05-15 98 views
3

目前我正在試圖以301重定向添加到我的路線在MVC試圖將功能添加到MvcHandler

做到這一點我試圖從MvcHandler繼承。 處理程序以正確的值立即獲取。但我永遠不能調試重寫的方法。

有人可以告訴我一個這樣的工作嘗試嗎?在asp.net管只是似乎在做自己的事......

public class CodeHttpHandler : MvcHandler 
{ 
    public CodeHttpHandler(RequestContext p_requestContext) 
      : base(p_requestContext) 
    { 
    } 
    protected override void ProcessRequest(HttpContext p_httpContext) 
    { 
    } 
    protected override void ProcessRequest(HttpContextBase p_httpContext) 
    { 
    } 
} 

更新:

這是我迄今發現的解決方案:

public class CodeRouteHandler : IRouteHandler 
{ 
    public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
     return new CodeHandler(requestContext); 
    } 
} 

public class CodeRouteConstants 
{ 
    public const string CODE = "code"; 
    public const string REDIRECT = "Redirect"; 
} 

public class CodeHandler : MvcHandler 
{ 
    public CodeHandler(RequestContext requestContext) 
     : base(requestContext) 
    { 
    } 

    private int? HandleCodedRoute(System.Web.HttpContextBase httpContext) 
    { 
     var context = httpContext.Request.RequestContext.RouteData; 
     if (context.DataTokens.ContainsKey(CodeRouteConstants.CODE)) 
     { 
      var statusCode = Int32.Parse(context.DataTokens[CodeRouteConstants.CODE] as string ?? "500"); 

      httpContext.Response.StatusCode = statusCode; 

      if (context.DataTokens.ContainsKey(CodeRouteConstants.REDIRECT)) 
      { 
       var redirectionMap = context.DataTokens[CodeRouteConstants.REDIRECT] as string ?? "404"; 

       foreach (var v in context.Values) 
       { 
        redirectionMap = redirectionMap.Replace(string.Format("{{{0}}}", v.Key), v.Value as string); 
       } 

       httpContext.Response.AddHeader("Location", redirectionMap); 
      } 

      httpContext.Response.End(); 
      return statusCode; 
     } 
     return null; 
    } 

    protected override System.IAsyncResult BeginProcessRequest(System.Web.HttpContext httpContext, System.AsyncCallback callback, object state) 
    { 
     var statusCode = HandleCodedRoute(new HttpContextWrapper(httpContext)); 
     if (statusCode.HasValue) 
     { 
      return null; 
     } 
     return base.BeginProcessRequest(httpContext, callback, state); 
    } 

    protected override System.IAsyncResult BeginProcessRequest(System.Web.HttpContextBase httpContext, System.AsyncCallback callback, object state) 
    { 
     return base.BeginProcessRequest(httpContext, callback, state); 
    } 

    protected override void ProcessRequest(System.Web.HttpContext httpContext) 
    { 
     base.ProcessRequest(httpContext); 
    } 
    protected override void ProcessRequest(System.Web.HttpContextBase httpContext) 
    { 
     base.ProcessRequest(httpContext); 
    } 
} 

回答

2

嘿,我也只是遇到了這個問題。 ProcessRequest()中的我的斷點從未被擊中。對於您的問題,我沒有全面的解決方案,但我確實有一個解決方法,可能會爲您提供所需的解決方案。嘗試從MvcHandler而不是ProcessRequest()覆蓋BeginProcessRequest()方法。如果您正在尋找的內容,上下文應該包含路由(操作和控制器)信息。

+0

好<我驗證了我確實在構造函數中獲得了正確的信息。一切都在那裏,而且據說,我找到了正確的處理程序。但我的邏輯根本不會被調用,我得到的默認行爲,而不是我編碼。 這讓我感到困惑 – 2010-05-16 13:15:02

+0

我有一個異步模型的問題 我目前得到「無法重定向HTTP頭後已發送。」錯誤。 你有沒有克服過這個? – 2010-05-17 13:36:28