面向CustomRouteHandler的问题,在asp.net webform项目中使用301 parmanent重定向

时间:2011-09-12 13:12:46

标签: asp.net routing

我设法为asp.net路由开发自定义路由处理程序。我开发处理301 parmanent重定向。这是我的完整代码

public class CustomRouteHandler : IRouteHandler
{
    public CustomRouteHandler(string virtualPath)
    {
        VirtualPath = virtualPath;
    }

    public string VirtualPath { get; private set; }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {

        if (requestContext.HttpContext.Request.Url.LocalPath.Contains("?"))
        { }

        requestContext.HttpContext.Response.Status = "301 Moved Permanently";
        requestContext.HttpContext.Response.StatusCode = 301;
        requestContext.HttpContext.Response.AppendHeader("Location", requestContext.HttpContext.Request.Url.LocalPath);


        var page = BuildManager.CreateInstanceFromVirtualPath
                       (VirtualPath, typeof(System.Web.UI.Page)) as IHttpHandler;

        return page;
    }

}

void Application_Start(object sender, EventArgs e)
    {

        RouteTable.Routes.Add("Source", new Route
        (
           "UrlRewrite/Approach1/Source/{ID}/{Title}",
           new CustomRouteHandler("~/UrlRewrite/Approach1/Source.aspx")
        ));

    }

实际上以下几行给了我这个问题             requestContext.HttpContext.Response.Status =“301永久移动”;             requestContext.HttpContext.Response.StatusCode = 301;             requestContext.HttpContext.Response.AppendHeader(“Location”,requestContext.HttpContext.Request.Url.LocalPath);

对于上述行,再次请求同一页面&再次就像无限循环一样。 所以请指导我做什么.....谢谢

0 个答案:

没有答案