2

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

Community
  • 1
  • 1
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110

3 Answers3

2

You also need to include the namespace of UrlEncoder class in your view:

@using Mynamespace
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
1

I encountered a similar error when calling a UrlHelper extension method from my view, but the solution was slightly different so I'll share it in case it helps someone else:

In my extension class, I needed to replace using System.Web.Http.Routing; with using System.Web.Mvc;

Both resolve UrlHelper, but the MVC reference is what you need to use it in your view.

Elliot Starks
  • 153
  • 1
  • 5
  • 15
0

Pass the interface(IUrlHelper) instead of class name(UrlHelper) as first parameter.

public static class UrlEncoder  
    {  
        public static string ToFriendlyUrl(this **IUrlHelper** helper,
            string urlToEncode)  
         {  
         //your code  
        }  
}
  • Please read the [editing help](https://stackoverflow.com/editing-help) - in order to format code as code, you only need to mark it with 3 backticks or to indent it. Could you please [edit] your answer and format it correctly. – David Buck Oct 08 '20 at 11:22