I'm using this class for having clean URLs in my application :
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();
}
}
and I'm using above class with this way :
<a href="/Products/@item.Id/@Url.ToFriendlyUrl(item.Name)">@item.Name</a>
but I'm getting this error and extension not working:
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?)
I've added these using directive :
using System;
using System.Text;
using System.Web.Mvc;
I tried this method but still I have same error :
@UrlHelper.ToFriendlyUrl(item.Name)
and used this directive using System.Web.Http.Routing;
instead using System.Web.Mvc;
but still I have same error.
it seems that UrlHelper
belongs to another assembly , I don't know.
any Ideas?
Thanks in your Advise