C# ResolveUrl 和 ResolveClientUrl 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1874636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between ResolveUrl and ResolveClientUrl?
提问by Shantanu Gupta
I have been using ResolveUrl for adding CSS and Javascript in ASP.NET files.
我一直在使用 ResolveUrl 在 ASP.NET 文件中添加 CSS 和 Javascript。
But I usually see an option of ResolveClientUrl. What is the difference between both?
但是我通常会看到一个 ResolveClientUrl 的选项。两者有什么区别?
When should I use ResolveClientUrl?
我什么时候应该使用 ResolveClientUrl?
采纳答案by Brandon
ResolveUrl creates the URL relative to the root.
ResolveUrl 创建相对于根的 URL。
ResolveClientUrl creates the URL relative to the current page.
ResolveClientUrl 创建相对于当前页面的 URL。
You can also use whichever one you want, however ResolveUrl is more commonly used.
你也可以使用任何你想要的,但是 ResolveUrl 更常用。
回答by Juri
According to the MSDN documentation:
根据 MSDN 文档:
A fully qualified URL to the specified resource suitable for use on the browser.
Use the ResolveClientUrl method to return a URL string suitable for use by the client to access resources on the Web server, such as image files, links to additional pages, and so on.
适合在浏览器上使用的指定资源的完全限定 URL。
使用 ResolveClientUrl 方法返回适合客户端使用的 URL 字符串来访问 Web 服务器上的资源,例如图像文件、指向其他页面的链接等。
The converted URL.
If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL.
For example, consider the following scenario:
A client has requested an ASP.NET page that contains a user control that has an image associated with it.
The ASP.NET page is located at /Store/page1.aspx.
The user control is located at /Store/UserControls/UC1.ascx.
The image file is located at /UserControls/Images/Image1.jpg.
If the user control passes the relative path to the image (that is, /Store/UserControls/Images/Image1.jpg) to the ResolveUrl method, the method will return the value /Images/Image1.jpg.
转换后的网址。
如果relativeUrl 参数包含绝对URL,则返回该URL 不变。如果 relativeUrl 参数包含相对 URL,则该 URL 将更改为对当前请求路径正确的相对 URL,以便浏览器可以解析该 URL。
例如,请考虑以下场景:
客户端请求了一个 ASP.NET 页面,该页面包含一个用户控件,该控件具有与其关联的图像。
ASP.NET 页面位于 /Store/page1.aspx。
用户控件位于 /Store/UserControls/UC1.ascx。
图像文件位于 /UserControls/Images/Image1.jpg。
如果用户控件将图像的相对路径(即 /Store/UserControls/Images/Image1.jpg)传递给 ResolveUrl 方法,该方法将返回值 /Images/Image1.jpg。
I think this explains it quite well.
我认为这很好地解释了它。
回答by LDawggie
Here's a simple example:
这是一个简单的例子:
//Returns: ../HomePage.aspx
String ClientURL = ResolveClientUrl("~/HomePage.aspx");
//Returns: /HomePage.aspx
String RegURL = ResolveUrl("~/HomePage.aspx");
//Returns: C:\inetpub\wwwroot\MyProject\HomePage.aspx
String ServerMappedPath = Server.MapPath("~/HomePage.aspx");
//Returns: ~/HomePage.aspx
String appRelVirtPath = AppRelativeVirtualPath;
//Returns: http://localhost:4913/
String baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
//Returns: "http://localhost:4913/HomePage.aspx"
String absUri = Request.Url.AbsoluteUri;
回答by Zolfaghari
In short:
简而言之:
Page.ResolveUrl(~): creates the URL from the root of app.
and
和
Page.ResolveClientUrl(~): creates the URL relative to the current page.(e.g: ../../..)
but in my tests in asp.net, Page.ResolveUrl is betterbecause of stable output & is not relative.
但在我在 asp.net 中的测试中,Page.ResolveUrl 更好,因为稳定的输出 & 不是相对的。
回答by Hasan Abbas
Using Page.ResolveUrl is better if you are trying to get a Javascript friendly Url.
如果您想获得一个 Javascript 友好的 Url,则使用 Page.ResolveUrl 会更好。
Like if you are opening an iframe from the parent page, you would need a full url that would be passed to the iframe src property.
就像从父页面打开 iframe 一样,您需要一个完整的 url,它将传递给 iframe src 属性。