【问题标题】:ASP.NET IIS7 Dynamic 404 PageASP.NET IIS7 动态 404 页面
【发布时间】:2011-01-23 11:59:43
【问题描述】:

我正在寻找动态 404 页面的解决方案。

我有一个Page404.aspx 页面,它在加载时请求查询字符串中的 WebsiteID 参数。

假设每个网站都有不同的 404 页面 html 存储在 DB 中,并且在每个页面加载时,它将通过 QueryString 中的 WebsiteID 显示正确的 html。

现在棘手的事情 - 如何使用当前的 WebsiteID 将客户端重定向到正确的 404 页面?

希望我已经足够清楚了。提前致谢,

加。

【问题讨论】:

    标签: asp.net iis-7 http-status-code-404


    【解决方案1】:

    如果您没有 Global.asax,请为您的网站创建一个。 这假设您的网站是按目录划分的。

    在新的 Global.asax 文件中应该有

    protected void Application_Error(Object sender, EventArgs e)
    {
       HttpContext ctx = HttpContext.Current;
    
       Exception exception = ctx.Server.GetLastError ();
    
    if (ex.GetType() == typeof(HttpException))
    {
       HttpException httpEx = (HttpException)ex;
       if(httpEx.GetHttpCode() == 404) 
       {
    
         int websiteID = FindWebsiteID(ctx.Request.Url.ToString());
    
         string errorPage = string.Format("~/404Page.aspx?={0}",websiteID);
    
         Response.Redirect(errorPage);
      }
    }
    
    public int FindWebsiteID(string url){
    
    //parse the url for the directory and look up the id
    //for the website via the directory name
    
    }
    

    您也可以查看this article 了解更多信息。

    【讨论】:

    • 但这会将每个错误 404 发送到id=2 - 如果我的网站是 id 46 怎么办?通过在配置文件中对其进行硬编码,您不会让它在每个网站上都是动态的。
    • 不是每个网站都有自己的配置吗?
    • 我不相信所有页面都有服务器范围的 404 重定向选项,我很确定必须为每个网站配置它。我能想到的唯一方法是修改 machine.config,但它不是动态的。
    • 所有网站都在同一个应用程序下运行 - 同一个 web.config
    猜你喜欢
    • 1970-01-01
    • 2010-12-15
    • 2011-08-01
    • 2023-03-10
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 2011-01-05
    • 2011-04-03
    相关资源
    最近更新 更多