ResolveUrl将URL转换为在请求客户端可用的URL(转From MSDN)

    技术2022-05-11  137

    将 URL 转换为在请求客户端可用的 URL。

    Public Function ResolveUrl( _ ByVal relativeUrl As String _ ) As String

    public string ResolveUrl( string relativeUrl );

    public: String* ResolveUrl( String* relativeUrl );

    public function ResolveUrl( relativeUrl : String ) : String;

    参数

    relativeUrl 与 TemplateSourceDirectory 属性相关联的 URL。

    返回值

    转换后的 URL。

    异常

    异常类型条件ArgumentNullException当 relativeUrl 参数包含空引用(Visual Basic 中为 Nothing)时发生。

    备注

    如果 relativeUrl 参数包含绝对 URL,则该 URL 原样返回。如果 relativeUrl 参数包含相对 URL,则该 URL 将更改为与当前请求路径相符的相对 URL,这样浏览器便能够解析该 URL。

    例如,请考虑以下方案:

    客户端已请求了一个 ASP.NET 页,该页含有一个用户控件,该用户控件有一个关联的图像。 ASP.NET 页位于 /Store/page1.aspx。 用户控件位于 /Store/UserControls/UC1.ascx。 图像文件位于 /UserControls/Images/MyPhoto1.jpg。

    如果用户控件将图像的相对路径(即 /Store/UserControls/Images/MyPhoto1.jpg)传递给 ResolveUrl 方法,则该方法将返回值 /UserControls/Images/MyPhoto1.jpg。

    此方法使用 TemplateSourceDirectory 属性解析为绝对 URL。返回的 URL 适用于客户端。

    示例

    下面的示例创建一个 Image Web 服务器控件对象,并使用 ResolveUrl 方法设置该图像的路径(该路径由 ImageUrl 属性存储)。

    ' Use the Context property to write text to the Response object ' associated with the current request. Context.Response.Write("<br><br>ParentControl's OnBubbleEvent called.") Context.Response.Write(("<br>Source of event is: " + sender.ToString()))

    // Use the Context property to write text to the Response object // associated with the current request. Context.Response.Write("<br><br>ParentControl's OnBubbleEvent called."); Context.Response.Write("<br>Source of event is: " + sender.ToString());

    // Use the Context property to write text to the Response Object* // associated with the current request. Context->Response->Write(S"<br><br>ParentControl's OnBubbleEvent called."); Context->Response->Write(String::Concat(S"<br>Source of event is: ", sender));

    没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

    要求

    平台: Windows 2000, Windows XP Professional, Windows Server 2003 系列


    最新回复(0)