是否有一个内置的创建绝对(完全限定)URL从一个相对路径,如"〜/ page.aspx"鉴于目前的网址是什么? [英] Is there a built-in create an absolute (fully qualified) url from a relative path such as "~/page.aspx" given the current URL?

查看:123
本文介绍了是否有一个内置的创建绝对(完全限定)URL从一个相对路径,如"〜/ page.aspx"鉴于目前的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情景是我有一个应用程序相对URL,如〜/路径/要/ page.aspx?查询=字符串。我需要以编程方式创建一个Web请求到该页面与目前使用 WebRequest.Create 。问题是WebRequest.Create需要一个完全合格的URL包括协议/域/端口等。

Scenario is I have a application relative url like "~/path/to/page.aspx?query=string". I need to programatically create a web request to that page and currently using WebRequest.Create. The problem is WebRequest.Create requires a fully qualified url including the protocol/domain/port etc.

我有机会获得当前Request.Url对象,但似乎没有成为一个简单的方法来得到的只是基本URL保持协议(HTTP VS HTTPS),以及任何端口号以及路径应用程序。

I have access to the current Request.Url object but there doesn't seem to be an easy way to get just the base url keeping the protocol (HTTP vs HTTPS) as well as any port numbers as well as the path to the application.

我的意思是所有的信息在那里,所以如果需要的话我可以把所有的部件,并结合他们,但现在看来似乎可能是容易出错,这将是巨大的,有一些内置了的行之有效的做工作。 Page.ResolveUrl 让我几乎没有,但它缺少协议和域/端口。

I mean all the info there, so if need be I could just take all the parts and combine them but it seems like it might be error prone and it would be great to have something built-in that's well tested to do the job. Page.ResolveUrl gets me almost there, but it's missing the protocol and the domain/port.

推荐答案

尝试 System.Web.VirtualPathUtility.ToAbsolute()。我用的(尽管在VB.NET,但我敢肯定有一个C#当量),成功率很大。它甚至可以转换只是一个基础虚拟路径(即 System.Web.VirtualPathUtility.ToAbsolute(〜)会变成你的应用程序的基本URL)。

Try System.Web.VirtualPathUtility.ToAbsolute(). I've used that (albeit in VB.NET, but I'm sure there's a C# equivalent) with a great deal of success. It can even translate just a base virtual path (i.e. System.Web.VirtualPathUtility.ToAbsolute("~") will turn into the base url of your app).

修改

如何 Request.Url.GetLeftPart(UriPartial.Authority)?这应该得到的URL计划和管理局的部分(我相信你指的是协议和域)。这里的MSDN文档:<一href=\"http://msdn.microsoft.com/en-us/library/system.uri.getleftpart.aspx\">http://msdn.microsoft.com/en-us/library/system.uri.getleftpart.aspx

How about Request.Url.GetLeftPart(UriPartial.Authority)? That should get the Scheme and Authority parts of the Url (which I believe are the protocol and domain you refer to). Here's the MSDN doc: http://msdn.microsoft.com/en-us/library/system.uri.getleftpart.aspx

这篇关于是否有一个内置的创建绝对(完全限定)URL从一个相对路径,如&QUOT;〜/ page.aspx&QUOT;鉴于目前的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆