Ybbest
Ybbest

Reputation: 1550

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

I would like to generate a static URL based on a few parameters. The page serve 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

Upvotes: 2

Views: 409

Answers (3)

Samuel Neff
Samuel Neff

Reputation: 74909

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();

Upvotes: 1

citronas
citronas

Reputation: 19365

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

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74909

What's wrong with using a relative url?

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

Much simpler.

Upvotes: 0

Related Questions