asp.net mvc url 重写后 不存在页面返回302解决方案

web.config里面添加:

<system.web>
    <customErrors mode=”On”   redirectMode=”ResponseRewrite“ > <!– 一定是ResponseRewrite,否则HTTP头返回的HTTPStatusCode还是302 –>
    <error statusCode=”404″ redirect=”/FileNotFound.aspx” /> <!– 一定得是aspx文件,html文件虽然也可以,但返回的StatusCode是302,用aspx就是为了修改HTTPStatusCode为404 –>
</customErrors>

</system.web>

然后,在网页根目录(根据上面的redirect指向的文件目录)创建文件FileNotFound.aspx,内容如下:

<%@ Page Language=”C#” Inherits=”System.Web.UI.Page” %>
<script runat=”server”>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Response.Status = “404 No Found”;    //把StatusCode重写为404
    }
}
</script>
<!– 以下的HTML代码可以替换成你想自定义404页面的HTML代码 –>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml” >
    <head id=”Head1″ runat=”server”>
        <title>页面不存在</title>
    </head>
    <body>
        页面不存在!<a href=”http://www.yourdomain.com”>点击返回</a>
    </body>
</html> 

你可能感兴趣的:(asp.net)