IHttpHandler 和相对 URL 的问题

发布于 2024-09-03 09:32:28 字数 823 浏览 1 评论 0原文

我开发了一个 IHttpHandler 类,并将其配置为 verb="*" path="*",所以我用它处理所有请求尝试为动态生成 html 的测试网站创建我自己的 REST 实现。

因此,当对 .css 文件的请求到达时,我必须执行诸如 context.Response.WriteFile(Server.MapPath(url)) 之类的操作...对于图片等也是如此,我一切都得自己回应。

我的主要问题是当我将相对 URL 放入锚点时;例如,我的主页带有这样的链接 Go to Page 1 ,在第 1 页中我有另一个链接 < ;a href="page2">转到第 2 页。第 1 页和第 2 页应该处于同一级别(http://host/page1http://host/page2,但是当我单击 Go to第 2 页,我在处理程序中得到了这个 url:~/page1/~/page2 ...这很痛苦,因为我必须执行 url = url.SubString(url.LastIndexOf(' 〜'))对于干净它,虽然我觉得没有什么问题,这种行为完全正常,

现在我可以应付,但我认为将来这会给我带来一些头痛。尝试使用context.Request.Url的信息设置所有带有绝对URL的链接,但这也很痛苦:D,所以我想知道是否有更好的方法来做到这一点 不要犹豫,

给我相当明显的答复,因为我在 Web 开发方面还很陌生,可能我会跳过一些关于 URL、Http 等的基本知识,

谢谢。

I've developed a IHttpHandler class and I've configured it as verb="*" path="*", so I'm handling all the request with it in an attempt of create my own REST implementation for a test web site that generates the html dynamically.

So, when a request for a .css file arrives, I've to do something like context.Response.WriteFile(Server.MapPath(url)) ... same for pictures and so on, I have to response everything myself.

My main issue, is when I put relative URLs in the anchors; for example, I have main page with a link like this <a href="page1">Go to Page 1</a> , and in Page 1 I have another link <a href="page2">Go to Page 2</a>. Page 1 and 2 are supposed to be at the same level (http://host/page1 and http://host/page2, but when I click in Go to Page 2, I got this url in the handler: ~/page1/~/page2 ... what is a pain, because I have to do an url = url.SubString(url.LastIndexOf('~')) for clean it, although I feel that there is nothing wrong and this behavior is totally normal.

Right now, I can cope with it, but I think that in the future this is gonna bring me some headache. I've tried to set all the links with absolute URLs using the information of context.Request.Url, but it's also a pain :D, so I'd like to know if there is a nicer way to do these kind of things.

Don't hesitate in giving me pretty obvious responses because I'm pretty new in web development and probably I'm skipping something basic about URLs, Http and so on.

Thanks in advance and kind regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2024-09-10 09:32:29

首先,我会查看传送到浏览器的输出 HTML,特别是您所描述的链接。

您说链接是 Go to Page 2 但根据您的结果,我猜它更像是 转到第 2 页
您可以通过在处理程序中放置一个制动点来确认这一点,当它用“~/page1/~/page2”触发时,请查看浏览器的地址栏,它应该显示类似“http://www.example.com/”的内容page1/~/page2"

您应该首先查看生成链接的代码。如果它是通过某种函数调用生成的,请确保您获取的是网址而不是脚本地址。

在任何情况下,这些在第一级页面之间切换的链接都应以“/”开头,表明它们的位置相对于网站的根目录,而不是相对于当前显示的页面。

First of all I would take a look at the output HTML delivered to the browser and specifically the links that you are describing.

You say that the link is <a href="page2">Go to Page 2</a> but according to your result I would guess it is more like <a href="~/page2">Go to Page 2</a>.
You can confirm this by placing a brakepoint in the handler and when it triggers with "~/page1/~/page2" look in the address bar of your browser and it should say something like "http://www.example.com/page1/~/page2"

You should first look at the code generating the link. If it is generated from some kind of function call, make sure you get the web address and not the script address.

In any case these kind of links that switch between first level pages should all start with a "/" indicating that their location is relative to the root of your website rather than relative to the current shown page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文