【问题标题】:C# I want the url, not the physical pathnameC# 我想要 url,而不是物理路径名
【发布时间】:2010-08-18 10:14:55
【问题描述】:

对于这行代码;

string link = HttpContext.Current.Server.MapPath("/Contract/Details/" + this.ContractId.ToString());

我得到了 C 盘上的物理路径名。

我要的是url,即

http://localhost:1234/Contract/Details/1

我如何得到这个?

【问题讨论】:

    标签: c# url uri


    【解决方案1】:
    // Use the Uri constructor to form a URL relative to the current page
    Uri linkUri = new Uri(HttpContext.Current.Request.Url, "/Contract/Details/" + this.ContractId.ToString());
    string link = linkUri.ToString();
    

    【讨论】:

      【解决方案2】:

      试试这个:

      string url = HttpContext.Current.Request.Url.AbsoluteUri;
      

      【讨论】:

      • 这将返回当前请求的 URL,可能这不是“/Contract/Details/1”
      【解决方案3】:

      .Net 路径上有一篇很棒的文章@http://west-wind.com/weblog/posts/132081.aspx

      查看 Url 或 PathInfo 属性。

      【讨论】:

        【解决方案4】:

        Uri base = new Uri("http://localhost:1234/";);

        Uri 文件 = new Uri(host, "/Contract/Details/" + this.ContractId.ToString());

        字符串 URL = file.AbsoluteUri;

        【讨论】:

        • Tim Robinson 的回答更好,因为他正在使用当前请求主机
        猜你喜欢
        • 2018-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-04
        • 1970-01-01
        • 2015-03-04
        • 2020-07-06
        相关资源
        最近更新 更多