Domanda

I'm developing an application with ASP.NET MVC 4 and I wanna my URLs look like below :

http://www.mysite.com/Product/Category/2/دربهای-چوبی

to do that I'm using this kind of URL pattern in Global.asax :

routes.MapRoute(
                name:"ViewProduct",
                url:"Product/Category/{Id}/{productName}",
                defaults: new { controller = "Product", action = "Category", id = UrlParameter.Optional, productName = "" }
            );

and in my view I'm using generating my URLs with this way :

<a href="/Product/Category/@item.Id/@Url.ToFriendlyUrl(item.Name)">@item.Name</a>

as you can see I'm create extension method called ToFriendlyUrl :

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();
        }

above way works correctly for English URLs(productName's segment) :

http://www.mysite.com/Product/Category/3/category-type-one

but for Persian URLs I'm getting this URL for example :

http://www.mysite.com/Product/Category/1/--------------

for example second one should have be:

http://www.mysite.com/Product/Category/2/دربهای-چوبی

How can I change the code to work with Persian URLs?

PS : I know in Persian language we don't have lower and upper cases and I omitted this line of code :

urlToEncode = (urlToEncode ?? "").Trim().ToLower();

But still I've same problem.

Thanks

È stato utile?

Soluzione

This if

                if ((ch >= '0' && ch <= '9') ||
                    (ch >= 'a' && ch <= 'z'))

means only English numbers and characters are allowed. But Persian characters have a different range:

 public static bool ContainsFarsi(string txt)
        {
            return !string.IsNullOrEmpty(txt) &&
                    Regex.IsMatch(txt, @"[\u0600-\u06FF]");
        }

+ Don't use that ToFriendlyUrl method. Create an extension method to apply your filtering and then use the standard Html.ActionLink method and pass your parameters as new route values or it's better to use the T4MVC HTML helper methods. By constructing your links manulally, you will lose a lot of features like adjusting the root path based on the current domain or sub domain and also encoding the special characters and many outer built-in features.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top