【问题标题】:how to manipulate the URL path in c# asp.net?如何在 c# asp.net 中操作 URL 路径?
【发布时间】:2012-11-07 15:31:34
【问题描述】:

我有以下路径:

-http://localhost/portal/reportview.aspx

但我需要检索前 3 部分:

-http://localhost/portal/

我试过HttpContext.Current.Request.Url.GetLeftPart();,但没有成功。

【问题讨论】:

  • 如果您在 Request.Url 上放置一个调试中断,您可以看到所有可以给您的项目,例如 .Path 并且可以完成这项工作。比提出问题要快。
  • 总是前三个部分还是删除“reportview.aspx”?或者换一种说法,如果url是http://localhost/portal/sub/page.aspx呢?

标签: c# asp.net


【解决方案1】:

试试这个;

string s = "http://localhost/portal/reportview.aspx";
string y = string.Format("{0}//{2}/{3}/",s.Split('/'));

【讨论】:

  • Split 在// 之间找到一个空字符串,所以要么移动索引,要么在Split 中使用StringSplitOptions.RemoveEmptyEntries。
【解决方案2】:

你可以使用子字符串。

string str = "http://localhost/portal/reportview.aspx";

string left = str.Substring(0, str.LastIndexOf("/"));

【讨论】:

    猜你喜欢
    • 2010-12-15
    • 2017-07-02
    • 2013-11-13
    • 2011-10-10
    • 2011-09-23
    • 1970-01-01
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多