帮助生成“友好的url”;在Razor (c# /MVC 4)
本文关键字:Razor MVC url 友好的 帮助 | 更新日期: 2023-09-27 18:18:10
目标
在Razor中创建一些将字符串转换为友好字符串的东西(我使用c#/MVC4)
我有以下代码:
<li>
<img
src="~/Images/Products/@Model["CategoryName"]/@Model["ProductThumbnailUrl"]"
/>
</li>
我的类别的名称是:CDs, DVDs and Bluray
,我想连接为"友好字符串",像cds-dvds-and-blurays
使用这样的东西:
<li>
<img
src="~/Images/Products
/@HtmlHelper.ConvertToFriendlyString(@Model["CategoryName"])
/@Model["ProductThumbnailUrl"]"
/>
</li>
你们都能明白吗?
那么,有人有什么想法吗?
我以前这样做过。在我挖掘代码时,需要考虑以下事项:
- 确保你存储了你生成的url,这样你可以做碰撞检测;将字符串转换为友好的url几乎肯定会有损耗,所以您需要逻辑来解决冲突的名称。 你应该试着把变音符转换成更容易输入的字符。
- 考虑将url到资源的映射设置为1:多关系;如果你的资源的名字改变了,你可能想要生成一个新的URL 和有旧的URL重定向到新的。
UPDATE:这是我的代码。堆栈溢出方法是可以的,但我更喜欢我的;它没有使用一组字符替换,而是使用强大的。net Unicode库来创建更友好的字符:
public static string ConvertToFriendlyUrl(string text)
{
var decomposed = text.Normalize(NormalizationForm.FormKD);
var builder = new StringBuilder();
foreach (var ch in decomposed)
{
var charInfo = CharUnicodeInfo.GetUnicodeCategory(ch);
switch (charInfo)
{
// Keep these as they are
case UnicodeCategory.DecimalDigitNumber:
case UnicodeCategory.LetterNumber:
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.CurrencySymbol:
case UnicodeCategory.OtherLetter:
case UnicodeCategory.OtherNumber:
builder.Append(ch);
break;
// Convert these to dashes
case UnicodeCategory.DashPunctuation:
case UnicodeCategory.MathSymbol:
case UnicodeCategory.ModifierSymbol:
case UnicodeCategory.OtherPunctuation:
case UnicodeCategory.OtherSymbol:
case UnicodeCategory.SpaceSeparator:
builder.Append('-');
break;
// Convert to lower-case
case UnicodeCategory.TitlecaseLetter:
case UnicodeCategory.UppercaseLetter:
builder.Append(char.ToLowerInvariant(ch));
break;
// Ignore certain types of characters
case UnicodeCategory.OpenPunctuation:
case UnicodeCategory.ClosePunctuation:
case UnicodeCategory.ConnectorPunctuation:
case UnicodeCategory.Control:
case UnicodeCategory.EnclosingMark:
case UnicodeCategory.FinalQuotePunctuation:
case UnicodeCategory.Format:
case UnicodeCategory.InitialQuotePunctuation:
case UnicodeCategory.LineSeparator:
case UnicodeCategory.ModifierLetter:
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.OtherNotAssigned:
case UnicodeCategory.ParagraphSeparator:
case UnicodeCategory.PrivateUse:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.Surrogate:
break;
}
}
var built = builder.ToString();
while (built.Contains("--"))
built = built.Replace("--", "-");
while (built.EndsWith("-"))
{
built = built.Substring(0, built.Length - 1);
}
while (built.StartsWith("-"))
{
built = built.Substring(1, built.Length - 1);
}
return built;
}
public static string GetIncrementedUrl(string url)
{
var parts = url.Split('-');
var lastPortion = parts.LastOrDefault();
int numToInc;
bool incExisting;
if (lastPortion == null)
{
numToInc = 1;
incExisting = false;
}
else
{
if (int.TryParse(lastPortion, out numToInc))
{
incExisting = true;
}
else
{
incExisting = false;
numToInc = 1;
}
}
var fragToKeep = incExisting
? string.Join("-", parts.Take(parts.Length - 1).ToArray())
: url;
return fragToKeep + "-" + (numToInc + 1).ToString();
}
public static string SeekUrl(
string name, Func<string, bool> uniquenessCheck)
{
var urlName = UrlUtils.ConvertToFriendlyUrl(name);
while (!uniquenessCheck(urlName))
{
urlName = UrlUtils.GetIncrementedUrl(urlName);
}
return urlName;
}