web config 设置
<system.web> <customErrors mode="On" defaultRedirect="error.asp" redirectMode="ResponseRewrite"> <error statusCode="404" redirect="404.aspx" /> </customErrors> </system.web> <system.webServer> <httpErrors errorMode="Custom"> <!--有3中 Custom, DetailedLocalOnly, Detailed--> <remove statusCode="404" /> <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" /> </httpErrors> </system.webServer>
在404.aspx.cs
protected void Page_Load(object sender, EventArgs e) { Response.StatusCode = 404; }
如果是自己处理route Uri 的话
try { throw new HttpException(404, "PageNotFound"); //可以 throw to 404page } catch (Exception ex) { throw ex; }