C# 404自定义重定向
您好,我正在尝试重定向,如果响应是404,但它没有按预期工作,您的专家能看到问题吗?。它仍然是通用的404 在我的Global.asax中C# 404自定义重定向,c#,asp.net,redirect,http-status-code-404,C#,Asp.net,Redirect,Http Status Code 404,您好,我正在尝试重定向,如果响应是404,但它没有按预期工作,您的专家能看到问题吗?。它仍然是通用的404 在我的Global.asax中 protected void Application_BeginRequest(Object sender, EventArgs e) { if (Response.Status == "404 Not Found") { string notFound = "~/custom_notfound.aspx
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (Response.Status == "404 Not Found")
{
string notFound = "~/custom_notfound.aspx";
Response.Redirect(notFound);
}
}
更新
到目前为止已经试过了
(Response.Status == "404 Not Found")
(Response.Status == "404")
(Response.StatusCode == 404)
您还可以使用web.config customerrors部分- e、 g.在system.web部分
<customErrors mode="On" defaultRedirect="/custom_error.aspx">
<error statusCode="404" redirect="/custom_notfound.aspx" />
</customErrors>
您可以添加到web.config以执行此重定向,您不需要使用
应用程序\u BeginRequest
来处理此重定向
看这个
如果你不能使用web.config,那么我会将你的启动页面设置为一个不存在的页面,在你的BeginRequest中设置一个断点,调试应用程序并查看请求,看看如何确定它是404。这将更容易确定最佳解决方案
再进一步了解一下,HttpWebResponse类中使用了一个。因此,使用不同的应用程序覆盖来获取默认响应,然后根据枚举检查其状态可能是有意义的。我认为BeginRequest可能不知道404错误。尝试实现应用程序错误。检查Server.GetLastError()是否为HttpException,如果是,请检查状态。您也可以使用web.config
<system.webServer>
<httpErrors errorMode="Custom" defaultResponseMode="File" >
<remove statusCode="404" />
<remove statusCode="500" />
<error statusCode="404" path="404.html" />
<error statusCode="500" path="500.html" />
</httpErrors>
</system.webServer>
您是否打算放置响应.重定向(未找到)代码>?啊,很抱歉在发布时输入错误,但仍然存在问题。谢谢你可以试试吗(Response.Status==404)(我不确定它是否有效,但试一下)试试。。仍然ir没有将其选中,而且它是(Response.StatusCode==404),因为(Response.Status==“需要一个字符串”),并且也尝试过(Response.Status==“404”)和尝试过(Response.Status==“404未找到”)在任何情况下都不起作用(Response.Status==HttpStatusCode.NotFound),出于某种原因,他们希望在asax中完成,在那里哪种方式可以做到?