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 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))
精彩评论