专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

iis html文件打开404,c# – 使用HttpModule处理html文件以捕获IIS7上的404错误

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:58:08       共计:3747 浏览
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace xland
{
    public class AspExceptionHandler : IHttpModule, IHttpHandler
    {
        public void Dispose() { }
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(ErrorHandler);
        }
        private void ErrorHandler(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            try
            {
                // Gather information
                Exception currentException = application.Server.GetLastError(); ;
                String errorPage = "http://www.begon.cn/error.aspx";
                HttpException httpException = currentException as HttpException;
                if (httpException == null || httpException.GetHttpCode() != 404)
                {
                    application.Server.Transfer(errorPage, true);
                }
                //The error is a 404
                else
                {
                    // Continue
                    application.Server.ClearError();
                    //Try and redirect to the proper page.
                    String requestedFile = application.Request.Url.AbsolutePath.Trim('/').Split('/').Last();
                    // Redirect if required
                    string redirectURL = requestedFile.Trim('/');
                    if (!string.IsNullOrEmpty(redirectURL))
                    {
                        //Redirect to the proper URL
                    }
                    //If we can't redirect properly, we set the statusCode to 404.
                    else
                    {
                        //Report the 404

                    }
                }
            }
            catch (Exception ex)
            {

            }

        }
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (!File.Exists(context.Request.PhysicalPath))
            {
                throw new HttpException(404, String.Format("The file {0} does not exist", context.Request.PhysicalPath));
            }
            else
            {
                context.Response.TransmitFile(context.Request.PhysicalPath);

            }
        }
    }
}

在ProcessRequest方法(IHttpHandler所需)中,我检查文件是否存在.

如果它不存在,我抛出一个由我的类的HttpModule部分捕获的HttpException.


版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:实现IHttpModule接口,给每个页面输出一段脚本 | ·下一条:详细解释 @media 属性与 (max-width:) and (min-width) 之间的关系及用法

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有