URL 中的尾部斜杠导致部分回发问题
.NET 3.5 应用程序用 C# 编写,带有 jQuery 和一些 ASP.NET AJAX UpdatePanel 风味。 我遇到了一个有趣的问题。我创建了一个由 LinkButton 组成的分页用户控件。每当有人单击页面、上一个、第一个或下一个按钮时,用户控件就会触发一个名为 CurrentPageChanged 的事件。然后,使用此分页控件的页面负责根据单击的分页控件项获取最新的记录集。
现在,我遇到的问题是: 如果我有这样的网址: http://localhost:2798/user/9794/profile,一切正常。但是,如果我有一个带有尾部斜杠的网址(即 http://localhost:2798/user/ 9794/profile/),我的 UpdatePanel 因 405 错误而摔倒。
有问题的例外是这样的:
[异常] Sys.WebForms.PageRequestManagerServerErrorException:Sys.WebForms.PageRequestManagerServerErrorException:在服务器上处理请求时发生未知错误。服务器返回的状态码为:405
现在,我通过Chrome的开发者工具查看了请求,我发现它正在请求这个url: http://localhost:2798/user/9794/profile/profile。看起来如果有一个尾部斜杠,它会附加一个额外的路径。
我有什么想法可以解决这个问题吗?
.NET 3.5 app written in C# here with both jQuery and some ASP.NET AJAX UpdatePanel flavouring.
I'm running into an interesting issue. I created a pagination user control that is made up of LinkButtons. The user control fires off an event called CurrentPageChanged whenever someone clicks on a page, previous, first, or next buttons. The page using this pagination control is then responsible for getting the newest set of records based on the pagination control item clicked.
Now, the issue I'm running into is this:
If I have a url like this: http://localhost:2798/user/9794/profile, everything works fine. However, if I have a url with the trailing slash (i.e. http://localhost:2798/user/9794/profile/), my UpdatePanel falls on its face with a 405 error.
The exception in question is this:
[Exception] Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 405
Now, I looked at the requests through Chrome's Developer Tools, and I see that it's requesting this url:
http://localhost:2798/user/9794/profile/profile. It looks like if there's a trailing slash, it'll append an extra path.
Any ideas how I can get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这就是相对 URL 的工作原理。浏览器使用尾部斜杠来决定 URL 是否引用文件夹(在这种情况下,URL 可以引用该文件夹内的另一个文件)或文件(在这种情况下,它必须查找父文件夹)。因此,尾部斜杠 URL 内的
确实会指向
.../profile/profile
。在您创建链接的任何地方(显式或通过 ASP.NET 控件)使用绝对 URL(或者更好的是根相对 URL,例如
href="/user/9794/profile"
)。相对 URL 是与 URL 的“路由”样式不兼容,其中 URL 中可能有可变数量的斜杠分隔的数据位和/或仅使用规范 URL,以便任何给定资源的 URL 始终是固定的;到“错误”版本,并在 URL 中添加额外的斜杠或其他多余内容,您将得到 301 重定向到“正确”版本。
Yes, that's how relative URLs work. A browser uses the trailing slash to decide whether the URL refers to a folder (in which case a URL could refer to another file inside that folder), or a file (in which case it would have to look up to the parent folder). So
<a href="profile">
inside the trailing-slash-URL will indeed be pointing at.../profile/profile
.Use absolute URLs (or, better, root-relative URLs like
href="/user/9794/profile"
everywhere you make a link (either explicitly or via an ASP.NET control). Relative URLs are incompatible with the ‘routed’ style of URL where you may have a variable number of slash-separated bits of data in the URL.And/or use only canonical URLs, so that the URL for any given resource is always fixed; if you go to the “wrong” version with an extra slash or other redundant stuff in the URL you get a 301 redirect to the “right” version.