当在页面上输出下面代码的时候就会自动调用 IIS 配置的 404 错误页面
Response.StatusCode = 404 ;
现在需求是
输出Http状态码为 404 , 同时不调用 IIS 配置的 404 错误页面,而是动态输出一些内容,这些内容是可以程序控制的。
这个如何做到??
如果能在程序集中控制到就好了
写了一个 DLL HTTPModule ,类似于反向代理
反向代理过来的网页可能是404的状态,同时会输出一个404 的自定义页面(美工设计过的)
当输出404状态的时候,
调用了IIS 自带的 404 错误页 输出不了源页面的 404 页面
把IIS里404功能禁用了,web.config里有可以设置404页面吧~
在Global.asax文件中定义,或者在Web.config中定义都可以的啊。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
namespace Travel.Web
{
public class Global : System.Web.HttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
try
{
if (Response.StatusCode == 404)
{
//自定义代码
//跳转页面
Response.Redirect("~/...");
}else{
//记录日志等...
}
}
catch (Exception ex)
{
Server.ClearError();
}
}
}
}
修改webconfig
<customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPages/Oops.aspx">
<error statusCode="404" redirect="~/ErrorPages/404.aspx" />
</customErrors>
在错误页面控制呀
这样就跳到错误页面了 想输出什么状态码都可以了