我需要调整我的代码,以便它会在数字后修剪网址.例如,我希望删除List = 36后的所有内容.字母和特殊字符是唯一可以跟随列表编号的潜在字符.所以我只想在列表编号后面读取字母或特殊字符时修剪网址.谢谢.
提交时捕获的当前URL的示例
https://portal.test/sites/test/testabc/Lists/TEST/DispFormSort.aspx?List=36caab7e%2D2234%2D4981%2D8225%%2Easpx
https://portal.test/sites/test/testabc/Lists/TEST/DispFormSort.aspx?List=36,http://portal.test/testabc/Lists/TEST/AllItems.aspx
Run Code Online (Sandbox Code Playgroud)
我希望在提交时捕获的网址:
https://portal.test.com/sites/test/testabc/Lists/RFC/DispFormSort.aspx?List=36
我当前捕获url的方法的代码:
public static string GenerateShortUrl(SPList list, int itemId)
{
try
{
var item = list.GetItemById(itemId);
var itemUrl = string.Empty;
if (item.File == null)
itemUrl = SPContext.Current.Web.Url + "/" + list.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + item.ID;
else
itemUrl = SPContext.Current.Web.Url + "/" + item.Url;
var generatedShortedUrl = GetShortUrl(itemUrl);
return generatedShortedUrl;
}
catch (Exception ex)
{
Utilities.WriteError(ex.ToString());
throw new SPException("An error occurred while attempting to shorten a URL.", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
private static string GetShortUrl(string itemUrl)
{
var shortId = Utilities.GenerateHash(itemUrl);
var shortenerUrl = string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
var farm = SPAdministrationWebApplication.Local;
using (var site = farm.Sites[0])
{
using (var web = site.RootWeb)
{
if (!web.Properties.ContainsKey("LiebrandUrlShortener"))
throw new SPException(SPUtility.GetLocalizedString("$Resources:liebrandurlshortener,CannotFindUrl", "liebrandurlshortener", web.Language));
var urlList = web.Lists[SPUtility.GetLocalizedString("$Resources:liebrandurlshortener,StorageName", "liebrandurlshortener", web.Language)];
var query = new SPQuery();
query.Query = "<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + shortId + "</Value></Eq></Where>";
var items = urlList.GetItems(query);
if (items.Count == 0)
{
var urlListItem = urlList.Items.Add();
urlListItem["Title"] = shortId;
urlListItem["RedirectTo"] = itemUrl;
web.AllowUnsafeUpdates = true;
urlListItem.Update();
web.AllowUnsafeUpdates = false;
}
shortenerUrl = web.Properties["LiebrandUrlShortener"];
}
}
});
return shortenerUrl + "/" + shortId;
}
public static string GenerateShortUrl(string longUrl)
{
return GetShortUrl(longUrl);
}
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Regex捕获所有内容,直到?List = [number].
你可以使用这个正则表达式:
Regex.Match(str, @"^.*\?List=\d+").Value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
919 次 |
| 最近记录: |