简体   繁体   English

ResolveUrl和ResolveClientUrl有什么区别?

[英]What is the difference between ResolveUrl and ResolveClientUrl?

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. 但我通常会看到ResolveClientUrl的选项。 What is the difference between both? 两者有什么区别?

When should I use ResolveClientUrl? 我什么时候应该使用ResolveClientUrl?

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更常用。

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;

According to the MSDN documentation: 根据MSDN文档:

ResolveClientUrl ResolveClientUrl

A fully qualified URL to the specified resource suitable for use on the browser. 适用于浏览器的指定资源的完全限定URL。

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. 使用ResolveClientUrl方法返回适合客户端使用的URL字符串,以访问Web服务器上的资源,例如图像文件,指向其他页面的链接等。

ResolveUrl RESOLVEURL

The converted URL. 转换后的网址。

If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. 如果relativeUrl参数包含绝对URL,则URL将保持不变。 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. 如果relativeUrl参数包含相对URL,则该URL将更改为对当前请求路径正确的相对URL,以便浏览器可以解析该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. 客户端已请求包含用户控件的ASP.NET页面,该用户控件具有与之关联的图像。

The ASP.NET page is located at /Store/page1.aspx. ASP.NET页面位于/Store/page1.aspx。

The user control is located at /Store/UserControls/UC1.ascx. 用户控件位于/Store/UserControls/UC1.ascx。

The image file is located at /UserControls/Images/Image1.jpg. 图像文件位于/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. 如果用户控件将图像的相对路径(即/Store/UserControls/Images/Image1.jpg)传递给ResolveUrl方法,则该方法将返回值/Images/Image1.jpg。

I think this explains it quite well. 我认为这很好地解释了它。

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 better because of stable output & is not relative. 但在我在asp.net的测试中, Page.ResolveUrl更好,因为输出稳定而且不相对。

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属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM