首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >缺少ASP.NET 404重定向查询字符串

缺少ASP.NET 404重定向查询字符串
EN

Stack Overflow用户
提问于 2012-09-17 17:31:40
回答 1查看 1.2K关注 0票数 1

使用我的web.config文件中的以下配置,

代码语言:javascript
复制
<customErrors mode="On" redirectMode="ResponseRedirect" >
    <error statusCode="404" redirect="/404" />
</customErrors>   
<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath="" path="/404" responseMode="ExecuteURL" />
</httpErrors>

404页面运行得很好,但是对于以.aspx结尾的url,会发生重定向,并且url的查询字符串将被删除。如果我将标记"customErrors“中的参数"redirectMode”更改为"ResponseRewrite“,它将不再对.aspx urls起作用,并且我只会得到默认的ASP.NET错误页面。我该如何解决这个问题呢?我需要在404页上的查询字符串,以便能够将用户重定向到正确的新url。

/Viktor

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-19 21:16:48

好的,最后写了我自己的http模块,

代码语言:javascript
复制
    public class FileNotFoundModule : IHttpModule
{
    private static CustomErrorsSection _configurationSection = null;
    private const string RedirectUrlFormat = "{0}?404;{1}";

    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(FileNotFound_Error);
    }

    private void FileNotFound_Error(object sender, EventArgs e)
    {
        var context = HttpContext.Current;
        if (context != null && context.Error != null)
        {
            var error = context.Error.GetBaseException() as HttpException;
            if (error != null && error.GetHttpCode() == 404 &&
                (ConfigurationSecion.Mode == CustomErrorsMode.On || (!context.Request.IsLocal && ConfigurationSecion.Mode == CustomErrorsMode.RemoteOnly)) &&
                !string.IsNullOrEmpty(RedirectUrl) && !IsRedirectLoop)
            {
                context.ClearError();
                context.Server.TransferRequest(string.Format(FileNotFoundModule.RedirectUrlFormat, RedirectUrl, context.Request.Url));
            }
        }
    }

    private bool IsRedirectLoop
    {
        get
        {
            var checkUrl = string.Format(FileNotFoundModule.RedirectUrlFormat,RedirectUrl,string.Empty);
            return HttpContext.Current.Request.Url.ToString().Contains(checkUrl);
        }
    }

    private CustomErrorsSection ConfigurationSecion
    {
        get
        {
            if (_configurationSection == null)
            {
                _configurationSection = (CustomErrorsSection)WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath).GetSection("system.web/customErrors");
            }
            return _configurationSection;
        }
    }

    private string RedirectUrl
    {
        get
        {
            foreach (CustomError error in ConfigurationSecion.Errors)
            {
                if (error.StatusCode == 404)
                {
                    return error.Redirect;
                }
            }
            return string.Empty;
        }
    }
}

适用于我:)

/Viktor

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12456532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档