How to change Url.Encode character replacement strategy in ASP.NET MVC?

I use Url.Encode in the view and replace the + so spaces instead:

/ production / cats-the-musical I get ... / cats + + music .

I am sure it is very simple, but where are you going to configure which symbols are used for this?

I will do it:

public static string EncodeForSEO(this UrlHelper helper, string unencodedUrl)
{
     return helper.Encode(unencodedUrl.Replace(' ', '-'));
}

Until I get the best answer from you guys.

Edit: Thanks to Guffa for pointing out my hasty coding.

+3
source share
2 answers

, UrlEncode, "+" URL-, "-" , . "-" , .

. Replace , .

public static string EncodeForSEO(this UrlHelper helper, string unencodedUrl) {
   return helper.Encode(unencodedUrl.Replace(' ', '-'));
}
+3

Path Query String

MVC / (), , . /Products.aspx?id=1 , MVC, /Products/View/1

, SEO , , /Products/View/1/Coffee

, - , , URL-, , " URL string .

  • UrlPathEncode()
  • ,
    • UrlPathEncode(), , .

: Url ( ) URL ( URL-).

  • cats the musical โ†’ UrlEncode โ†’ cats+the+musical - URL-.
  • cats the musical โ†’ UrlPathEncode โ†’ cats%20the%20musical

; Web Forms vs MVC - /Products.aspx?name=Coffee+Beans /Products/View/Coffee%20Beans

, , . : SEO ? Q: @Guffas, "" -" , UrlPathEncoding.

, , , SEO (, ), , , "-", ,

cats         the     musical
, cats-----the-----musical, cats-the-musical
+4

Source: https://habr.com/ru/post/1715778/


All Articles