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

根据Alexander的答案做了一点研究,我在AspExceptionHandler中实现了IHttpHandler.

我的课现在看起来像:

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://companywebsite.be/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();

String shouldMail404 = true;

//Try and redirect to the proper page.

String requestedFile = application.Request.Url.AbsolutePath.Trim('/').Split('/').Last();

// Redirect if required

String redirectURL = getRedirectURL(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)

{

ExceptionCatcher.FillWebException(HttpContext.Current, ref ex);

ExceptionCatcher.CatchException(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.

我的web.config中的system.webServer节点现在看起来像这样:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值