我使用這個class在我的應用程序有乾淨的URL擴展方法:得到錯誤,當我創建UrlHelper
public static class UrlEncoder
{
public static string ToFriendlyUrl(this UrlHelper helper,
string urlToEncode)
{
urlToEncode = (urlToEncode ?? "").Trim().ToLower();
StringBuilder url = new StringBuilder();
foreach (char ch in urlToEncode)
{
switch (ch)
{
case ' ':
url.Append('-');
break;
case '&':
url.Append("and");
break;
case '\'':
break;
default:
if ((ch >= '0' && ch <= '9') ||
(ch >= 'a' && ch <= 'z'))
{
url.Append(ch);
}
else
{
url.Append('-');
}
break;
}
}
return url.ToString();
}
}
和我在上面使用類是這樣的:
<a href="/Products/@item.Id/@Url.ToFriendlyUrl(item.Name)">@item.Name</a>
但我發現了這個錯誤,擴展不工作:
Compiler Error Message: CS1061: 'System.Web.Mvc.UrlHelper' does not contain a definition for 'ToFriendlyUrl' and no extension method 'ToFriendlyUrl' accepting a first argument of type 'System.Web.Mvc.UrlHelper' could be found (are you missing a using directive or an assembly reference?)
我這些使用DIRECTI添加五個:
using System;
using System.Text;
using System.Web.Mvc;
我試圖this method但我仍然有同樣的錯誤:
@UrlHelper.ToFriendlyUrl(item.Name)
,並使用這個指令using System.Web.Http.Routing;
代替using System.Web.Mvc;
但我仍然有同樣的錯誤。 看來UrlHelper
屬於另一個程序集,我不知道。
有什麼想法?
謝謝您的建議