【问题标题】:Trying to add functionality to MvcHandler尝试向 MvcHandler 添加功能
【发布时间】:2010-05-15 18:43:21
【问题描述】:

我目前正在尝试将 301 重定向添加到我在 MVC 中的路由

为此,我尝试从 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);
    }
}

【问题讨论】:

    标签: .net asp.net-mvc-2 c#-4.0


    【解决方案1】:

    嘿,我也遇到了这个问题。我在 ProcessRequest() 中的断点从未被击中。对于您的问题,我没有完整的解决方案,但我确实有一个解决方法,它可能会为您提供您想要的。尝试从 MvcHandler 覆盖 BeginProcessRequest() 方法而不是 ProcessRequest()。 Context 应该包含路由(动作和控制器)信息,如果这是您要查找的信息。

    【讨论】:

    • 好吧
    • 我的异步模型有问题,我目前收到“发送 HTTP 标头后无法重定向”。错误。你克服了吗?
    猜你喜欢
    • 2019-07-31
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2017-04-26
    • 2010-09-07
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多