开发者

How Can I generate a static url a file in asp.net

开发者 https://www.devze.com 2022-12-15 20:56 出处:网络
I would like to generate a static URL based on a few parameters. The page serv开发者_JS百科e the file for downloading is called CertificateDownload.aspx ,I am generating the download link in Report.as

I would like to generate a static URL based on a few parameters. The page serv开发者_JS百科e the file for downloading is called CertificateDownload.aspx ,I am generating the download link in Report.aspx.These 2 files reside on the same physical folder.I do not like the replace method ,but I could not think of another way of doing it. How can I improve my code or what is a better way of doing it.

I need the absolute url to be displayed as text in the web browser.

Dim downLoadUrl As String = HttpContext.Current.Request.Url.ToString.Replace("Report.aspx", "CertificateDownload.aspx") + "?CertificateID=" + CertificateName

HyperLinkDownloadLink.Visible = True
HyperLinkDownloadLink.Text = downLoadUrl
HyperLinkDownloadLink.NavigateUrl = downLoadUrl


You can do it cleanly with UriBuilder. Some people might say it's overkill, but it makes the intent of the code very clear and it's easier to program and maintain, and less error-prone.

    var uriBuilder = new UriBuilder(HttpContext.Current.Request.Url);
    uriBuilder.Path = Path.GetDirectoryName(uriBuilder.Path) + "/CertificateDownload.aspx";
    uriBuilder.Query = "CertificateID=" + CertificateName;
    var downloadUrl = uriBuilder.ToString();


What's wrong with using a relative url?

downLoadUrl = "CertificateDownload.aspx?CertificateID=" + CertificateName

Much simpler.


Request.MapPath(string.format("CertificateDownload.aspx?CertificateID={0}", CertificateName))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号