我希望缺少的页面的所有网址转发到我的404页面,这是我的根目录404error.aspx
问题
到目前为止,只有具有.aspx的URL才能正常工作.例如,如果输入4error.aspx,您将被重定向到错误页面.
- /404error.aspx?aspxerrorpath=/4error.aspx
背景
我不是.NET开发人员,所以我接手了这个使用经典的.aspx的项目.所以我没有使用任何Microsoft产品在模板或框架中构建任何这个.我只是在Sublime文本中编码.
我已经研究了
> 404 Redirecting for non aspx pages – 答案不完整或不工作.
> 404 does not append aspxerrorpath for non aspx pages – 但这是有点不同于我想要的.
我看到我在Google上发现的许多文章,但没有一个完整的例子,实际上有效.
码
这是我在web.config中开始的整个代码
web.config中
- <configuration>
- <system.web>
- <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
- <globalization
- fileEncoding="utf-8"
- requestEncoding="utf-8"
- responseEncoding="utf-8"
- culture="en-US"
- uiCulture="de-DE"
- />
- </system.web>
- </configuration>
我也从Microsoft发现了这个例子(404error.aspx是我的修改)
http://msdn.microsoft.com/en-us/library/vstudio/bb397417%28v=vs.100%29.aspx
web.config(2)
这也没有处理没有.aspx的页面
然后我尝试了这个例子
web.config(3)
- <?xml version="1.0"?>
- <configuration>
- <system.web>
- <customErrors mode="RemoteOnly" defaultRedirect="~/404error.aspx" />
- <error statusCode="404" redirect="~/404error.aspx" />
- <globalization
- fileEncoding="utf-8"
- requestEncoding="utf-8"
- responseEncoding="utf-8"
- culture="en-US"
- uiCulture="de-DE"
- />
- </system.web>
- <system.webServer>
- <httpErrors>
- <remove statusCode="401" subStatusCode="-1" />
- <remove statusCode="403" subStatusCode="-1" />
- <remove statusCode="404" subStatusCode="-1" />
- <remove statusCode="500" subStatusCode="-1" />
- <!-- full url when responsemode is Redirect -->
- <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
- <!-- local relative path when responsemode is ExecuteURL -->
- <error statusCode="403" path="~/404error.aspx" responseMode="ExecuteURL" />
- <error statusCode="404" path="~/404error.aspx" responseMode="ExecuteURL" />
- <error statusCode="500" path="~/404error.aspx" responseMode="ExecuteURL" />
- </httpErrors>
- <modules runAllManagedModulesForAllRequests="true"/>
- </system.webServer>
- </configuration>
显然,我必须改变404页以外的路径,但我只想测试这些错误.最后一个web.config工作更糟糕.
你能告诉我需要修改的内容吗?我会欣赏一个完整的<配置>因为我一直困惑什么在哪里.
编辑1
我读过很多开发人员建议将其作为HTML页面.所以现在我的页面是404.html这是我更新的web.config
- <configuration>
- <system.web>
- <customErrors mode="On"
- redirectMode="ResponseRewrite">
- <error statusCode="404"
- redirect="~/404.html"/>
- </customErrors>
- <globalization
- fileEncoding="utf-8"
- requestEncoding="utf-8"
- responseEncoding="utf-8"
- culture="en-US"
- uiCulture="de-DE"
- />
- </system.web>
- <system.webServer>
- <httpErrors errorMode="Custom"
- defaultResponseMode="File">
- <remove statusCode="404"/>
- <error statusCode="404"
- path="~/404.html"/>
- </httpErrors>
- </system.webServer>
- </configuration>
解决方法
您的第三次尝试“web.config(3)”和“编辑1”几乎在那里.问题是您不能在这里使用应用程序相对路径(例如:“〜/ 404.html”),它们必须与站点根目录相对(例如:“/404.html”).
- <?xml version="1.0"?>
- <configuration>
- <system.webServer>
- <httpErrors>
- <remove statusCode="401" subStatusCode="-1" />
- <remove statusCode="403" subStatusCode="-1" />
- <remove statusCode="404" subStatusCode="-1" />
- <remove statusCode="500" subStatusCode="-1" />
- <!-- full url when responsemode is Redirect -->
- <error statusCode="401" path="http://foo.com/default.htm" responseMode="Redirect" />
- <!-- local relative path when responsemode is ExecuteURL -->
- <error statusCode="403" path="/404error.aspx" responseMode="ExecuteURL" />
- <error statusCode="404" path="/404error.aspx" responseMode="ExecuteURL" />
- <error statusCode="500" path="/404error.aspx" responseMode="ExecuteURL" />
- </httpErrors>
- </system.webServer>
- </configuration>