我正在构建一个自定义的痕迹,我想要每个LinkButton唯一的commandArgument url。
我有一个通用的字符串变量,它是一个url。 每个子网站的名称可以是不同的,并且尽可能地不限制hierchy。
String变量可能如下所示:
http://site/org/content/Change/Book/process/item我想要做的是拆分字符串变量并将其添加到数组,所以它看起来像这样:
http://site/org http://site/org/content/ http://site/org/content/Change/ http://site/org/content/Change/Book/ http://site/org/content/Change/Book/process/ http://site/org/content/Change/Book/process/item我试过以下代码:
private void AddBreadCrumb(SPWeb web) { var webUrl = web.Url; var linkList = new List<string>(webUrl.Split('/')); // etc.. }但它并不像我希望它那样做。
适用任何形式的帮助
I am building doing a custom breadcrumb and I want to each LinkButton unique commandArgument url.
I have a generic string variable that is a url. The name of each subweb can be different and as long as possible the hierchy is not limited.
the String variable could look like this:
http://site/org/content/Change/Book/process/itemwhat I would like to do is to split the string variable and add it to a array so it looks like this:
http://site/org http://site/org/content/ http://site/org/content/Change/ http://site/org/content/Change/Book/ http://site/org/content/Change/Book/process/ http://site/org/content/Change/Book/process/itemI have tried following code:
private void AddBreadCrumb(SPWeb web) { var webUrl = web.Url; var linkList = new List<string>(webUrl.Split('/')); // etc.. }But it doesnt do like I want it to do.
Any kind of help is appriacted
最满意答案
您可以使用扩展方法和一些LINQ :
public static IEnumerable<string> ParseUrl(this string source) { if(!Uri.IsWellFormedUriString(source, UriKind.Absolute)) throw new ArgumentException("The URI Format is invalid"); var index = source.IndexOf("//"); var indices = source.Select((x, idx) => new {x, idx}) .Where(p => p.x == '/' && p.idx > index + 1) .Select(p => p.idx); // Skip the first index because we don't want http://site foreach (var idx in indices.Skip(1)) { yield return source.Substring(0,idx); } yield return source; }这是用法:
string url = "http://site/org/content/Change/Book/process/item"; var parts = url.ParseUrl();结果在LINQPad :
You can use an extension method for that and some LINQ:
public static IEnumerable<string> ParseUrl(this string source) { if(!Uri.IsWellFormedUriString(source, UriKind.Absolute)) throw new ArgumentException("The URI Format is invalid"); var index = source.IndexOf("//"); var indices = source.Select((x, idx) => new {x, idx}) .Where(p => p.x == '/' && p.idx > index + 1) .Select(p => p.idx); // Skip the first index because we don't want http://site foreach (var idx in indices.Skip(1)) { yield return source.Substring(0,idx); } yield return source; }Here is the usage:
string url = "http://site/org/content/Change/Book/process/item"; var parts = url.ParseUrl();Result in LINQPad:
更多推荐
发布评论