I have an ASP.NET page to handle "404 page not found" it's setup by configuring the <customErrors>
section in my web.config file and setting the custom error page for 404 errors in IIS manager.
我有一个ASP.NET页面来处理“找不到404页面”它的设置是通过在我的web.config文件中配置
It works as expected for pages in the root of my website but doesn't work in a subdirectory of the site - the hyperlinks and content in my custom error page have the wrong url base.
它对我网站根目录中的页面按预期工作,但在网站的子目录中不起作用 - 我的自定义错误页面中的超链接和内容具有错误的URL基础。
All the links in my error page are server-side controls (runat="server") and have their links based with "~/".
我的错误页面中的所有链接都是服务器端控件(runat =“server”),并且其链接基于“〜/”。
When I browse the site with http://mysite/nosuchfolder/nosuchfile
the page renders with links thinking it's being served from the root, rather from nosuchfolder
and as such all the links are broken in the browser as the browser is basing links from nosuchfolder
.
当我使用http:// mysite / nosuchfolder / nosuchfile浏览网站时,页面呈现的链接认为它是从根目录提供的,而不是来自nosuchfolder,因此浏览器基于nosuchfolder建立链接时所有链接都被破坏了。
Is there any way to 'tell' the ASP.NET page to re-base links on a different folder/filename?
有没有办法'告诉'ASP.NET页面重新建立不同的文件夹/文件名链接?
Notes:
- The majority of the page template is rendered from a master page
- I'm running IIS6 and have set custom error 404 to URL:
/error404.aspx
- In my web.config file I have configured the
<customErrors>
section to redirect to/error404.aspx
- As a workaround I am using the HTML
<base>
tag in the page, but I want to avoid this - I don't want to have to change all my "~/" based links to "/" or an in-line basing hack
大多数页面模板都是从母版页呈现的
我正在运行IIS6并将自定义错误404设置为URL:/error404.aspx
在我的web.config文件中,我已将
作为一种解决方法,我在页面中使用HTML
我不想将所有基于“/ /”的链接更改为“/”或内嵌基础黑客
2 个解决方案
#1
When you set uo yoru custome error pages, is it using redirection or URL rewriting? The problem you describe is a common issue when implementing URL rewriting in ASP.NET. Essentially, in URL rewriting it becomes unclear to the processor which URL to use when parsing URLs. A number if articles/posts have been written on how to address the issue.
当您设置uo yoru custome错误页面时,它是使用重定向还是URL重写?在ASP.NET中实现URL重写时,您描述的问题是一个常见问题。实质上,在URL重写中,处理器变得不清楚在解析URL时使用哪个URL。如果已经写了关于如何解决问题的文章/帖子的数字。
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
http://searchengineland.com/url-rewriting-custom-error-pages-in-aspnet-20-12234
#2
Scott Gu's article on URL rewriting put me on the right path (so to speak!) - thanks for the tip James.
Scott Gu关于URL重写的文章让我走上了正确的道路(可以这么说!) - 感谢James的提示。
The answer is to use Context.RewritePath(newPath)
答案是使用Context.RewritePath(newPath)
Here is the code I use in my custom 404 page -
这是我在自定义404页面中使用的代码 -
protected override void Render(HtmlTextWriter writer)
{
string rebase = Server.UrlDecode(Request.ServerVariables["QUERY_STRING"]);
if (rebase.Length>10 && rebase.StartsWith("404;"))
{
try
{
rebase = new Uri(rebase.Substring(4)).AbsolutePath;
}
catch
{
rebase = "/";
}
finally
{
Context.RewritePath(rebase);
}
}
base.Render(writer);
Response.StatusCode = 404;
}
To handle missing .aspx pages in the same way, there is a setting in web.config
called redirectMode
that you set to ResponseRewrite
要以相同的方式处理丢失的.aspx页面,web.config中有一个名为redirectMode的设置,您将其设置为ResponseRewrite
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/error404.aspx" />
</customErrors>
This stops the default behaviour of redirecting the user to ~/error404.aspx?aspxerrorpath=badfile
这会停止将用户重定向到〜/ error404.aspx的默认行为吗?aspxerrorpath = badfile
Note: this setting is new to the latest ASP.NET service packs (e.g. Framework 3.5 SP1)
注意:此设置是最新ASP.NET Service Pack的新增功能(例如Framework 3.5 SP1)
MSDN Reference for redirectMode
redirectMode的MSDN参考
#1
When you set uo yoru custome error pages, is it using redirection or URL rewriting? The problem you describe is a common issue when implementing URL rewriting in ASP.NET. Essentially, in URL rewriting it becomes unclear to the processor which URL to use when parsing URLs. A number if articles/posts have been written on how to address the issue.
当您设置uo yoru custome错误页面时,它是使用重定向还是URL重写?在ASP.NET中实现URL重写时,您描述的问题是一个常见问题。实质上,在URL重写中,处理器变得不清楚在解析URL时使用哪个URL。如果已经写了关于如何解决问题的文章/帖子的数字。
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
http://searchengineland.com/url-rewriting-custom-error-pages-in-aspnet-20-12234
#2
Scott Gu's article on URL rewriting put me on the right path (so to speak!) - thanks for the tip James.
Scott Gu关于URL重写的文章让我走上了正确的道路(可以这么说!) - 感谢James的提示。
The answer is to use Context.RewritePath(newPath)
答案是使用Context.RewritePath(newPath)
Here is the code I use in my custom 404 page -
这是我在自定义404页面中使用的代码 -
protected override void Render(HtmlTextWriter writer)
{
string rebase = Server.UrlDecode(Request.ServerVariables["QUERY_STRING"]);
if (rebase.Length>10 && rebase.StartsWith("404;"))
{
try
{
rebase = new Uri(rebase.Substring(4)).AbsolutePath;
}
catch
{
rebase = "/";
}
finally
{
Context.RewritePath(rebase);
}
}
base.Render(writer);
Response.StatusCode = 404;
}
To handle missing .aspx pages in the same way, there is a setting in web.config
called redirectMode
that you set to ResponseRewrite
要以相同的方式处理丢失的.aspx页面,web.config中有一个名为redirectMode的设置,您将其设置为ResponseRewrite
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/error404.aspx" />
</customErrors>
This stops the default behaviour of redirecting the user to ~/error404.aspx?aspxerrorpath=badfile
这会停止将用户重定向到〜/ error404.aspx的默认行为吗?aspxerrorpath = badfile
Note: this setting is new to the latest ASP.NET service packs (e.g. Framework 3.5 SP1)
注意:此设置是最新ASP.NET Service Pack的新增功能(例如Framework 3.5 SP1)
MSDN Reference for redirectMode
redirectMode的MSDN参考