2016-04-08 24 views

cevap

1

URL sümüklü böcekleri soruyorsunuzdur, bu durumda this answer.

Kod aşağıda yer almaktadır.

/// <summary> 
/// Produces optional, URL-friendly version of a title, "like-this-one". 
/// hand-tuned for speed, reflects performance refactoring contributed 
/// by John Gietzen (user otac0n) 
/// </summary> 
public static string URLFriendly(string title) 
{ 
    if (title == null) return ""; 

    const int maxlen = 80; 
    int len = title.Length; 
    bool prevdash = false; 
    var sb = new StringBuilder(len); 
    char c; 

    for (int i = 0; i < len; i++) 
    { 
     c = title[i]; 
     if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) 
     { 
      sb.Append(c); 
      prevdash = false; 
     } 
     else if (c >= 'A' && c <= 'Z') 
     { 
      // tricky way to convert to lowercase 
      sb.Append((char)(c | 32)); 
      prevdash = false; 
     } 
     else if (c == ' ' || c == ',' || c == '.' || c == '/' || 
     c == '\\' || c == '-' || c == '_' || c == '=') 
     { 
      if (!prevdash && sb.Length > 0) 
      { 
       sb.Append('-'); 
       prevdash = true; 
      } 
     } 
     else if ((int)c >= 128) 
     { 
      int prevlen = sb.Length; 
      sb.Append(RemapInternationalCharToAscii(c)); 
      if (prevlen != sb.Length) prevdash = false; 
     } 
     if (i == maxlen) break; 
    } 

    if (prevdash) 
     return sb.ToString().Substring(0, sb.Length - 1); 
    else 
     return sb.ToString(); 
} 

Sen RemapInternationalCharToAsciihere bulabilirsiniz.

Daha sonra bu satırlar boyunca bir şey yapacağını: Açıklamalarda sorunuzun Başına

string courseName = "Biology #101 - A Beginner's Guide!"; 

string urlFriendlyCourseName = URLFriendly(courseName); 
//outputs biology-101-a-beginners-guide 

Update URL içinde sizin CourseID içermekte olup DB girişini eşleştirmek için kullanabilirsin (http://mycourses.com/12345/biology-101-a-beginners-guide/ numaralı telefondan 12345 numaralı telefondan eşleştirin). URLFriendly(courseName) sonucunu, bir DB sütununda (örneğin, CourseSlug) saklamanız gerekir, böylece CourseID, CourseSlug veya her ikisi de eşleştirebilirsiniz.

+0

Paragrafını kodlamak için. Şimdi benim durumum, bu dizeyi url'den çözmek ve daha sonra Veritabanına göndermek istiyorum. başlığa göre, eğer başlık Db ile eşleşirse, bazı veriler iade edilecektir. nasıl halledilir –

+0

Güncellenen cevaba bir göz atın. – trashr0x

İlgili konular