如何打开一个相对URL为完整的URL? [英] How do I turn a relative URL into a full URL?
问题描述
这是可能与一个例子来解释更容易。我试图找到转向相对URL的方式,例如, /Foo.aspx或〜/ Foo.aspx成一个完整的URL,例如 HTTP://localhost/Foo.aspx 。当我部署的方式来测试或阶段,在该网站下运行的域是不同的,我会得到的http://test/Foo.aspx 和 HTTP://stage/Foo.aspx
This is probably explained more easily with an example. I'm trying to find a way of turning a relative URL, e.g. "/Foo.aspx" or "~/Foo.aspx" into a full URL, e.g. http://localhost/Foo.aspx. That way when I deploy to test or stage, where the domain under which the site runs is different, I will get http://test/Foo.aspx and http://stage/Foo.aspx.
任何想法?
推荐答案
有这个戏(修改<一个href=\"http://web.archive.org/web/20130730034405/http://www.aspdotnetfaq.com/Faq/how-to-convert-relative-url-to-absolute-url-in-asp-net-page.aspx\"相对=nofollow>从这里)
public string ConvertRelativeUrlToAbsoluteUrl(string relativeUrl) {
return string.Format("http{0}://{1}{2}",
(Request.IsSecureConnection) ? "s" : "",
Request.Url.Host,
Page.ResolveUrl(relativeUrl)
);
}
这篇关于如何打开一个相对URL为完整的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!