2010-05-15 91 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