2011-12-12 46 views
4

斜線和空格我有我的Global.asax(MVC 3 web項目)定義了以下路線:MVC 3 - 在航線

routes.MapRoute(
       "BlogCategory", // Route name 
       "Blog/Category/{*category}", // URL with parameters 
       new { controller = "Blog", action = "Index", category = "" } // Parameter defaults 
      ); 

而且我的行爲接受一個category參數,看起來像這樣:

public ViewResult Index(string category, int page = 1) 
     { 
      PostListViewModel viewModel; 
      if (string.IsNullOrEmpty(category)) 
      { 
....show all cats else show only the one passed in 

這可以正常工作,並將類別傳遞給控制器​​,適當地過濾我的結果。

我的問題是,當其中一類,我創建看起來像這樣爲它的類別名稱:

Projects/Lab

(注意空間和斜槓)

這將創建類似這樣的URL:

/Blog/Category/Projects%20/%20Lab

當我跟着鏈接,我得到這個錯誤:

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Blog/Category/Projects/Lab

它在調試時從不會到達索引操作。

我的問題是,我該如何做這項工作,或者當創建類別名稱以防止發生這種情況時,我必須進行一些輸入驗證?

+1

您是否考慮過傳遞類別ID? – Tyrsius

+0

您確定需要對輸入的值進行修剪。不只是檢查空格......有些字符不會顯示在URL(「Ñ」或其他非英文字符)中......請記住還有其他語言。所以,爲了避免這一切...我建議你去爲類別ID。 – Romias

回答

0

首先,感謝Tyrsius,Romias和eth0的建議。

我決定我不想使用Id作爲類別,但我不想創建路由處理程序,因爲這不是真的解決根本問題。

相反,我創建了一個名爲「UsedAsUrl」的驗證屬性,並將其應用於我的域模型中的Category.Name。 這具有內置驗證(對最終用戶有好處)的好處,並且從開發人員的角度來看具有良好的可重用性。

所以我的分類模型現在看起來是這樣(注意[UsedAsUrl]屬性):

public class Category 
{ 
     [Key] 
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
     public int Id { get; set; } 

     [Required] 
     [UsedAsUrl] 
     [MaxLength(50, ErrorMessage = "One word or groups of words please")] 
     public string Name { get; set; } 

     public virtual List<Post> Posts { get; set; } 
} 

和屬性我創造了這個樣子的:

using System; 
using System.ComponentModel.DataAnnotations; 
using System.Text.RegularExpressions; 

namespace CommonWebsiteUtilities.Attributes 
{ 
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)] 
    public class UsedAsUrlAttribute : ValidationAttribute 
    { 
     public UsedAsUrlAttribute() : base("Must not contain a slash with spaces either side") {} 

     public override bool IsValid(object value) 
     { 
      var input = value.ToString(); 
      var result = true; 

      if (input.Contains("/")) 
      { 
       input = Regex.Replace(input, @"\s+", " "); 
       if (input.Contains(" /")) result = false; 
       if (input.Contains("/ ")) result = false; 
       if (input.Contains("/")) result = false; 
      } 

      return result; 
     } 
    } 
} 

現在,當我去補充類別:

adding a category that I know will fail validation

我得到這樣的迴應奧波atically:

Failed response with validation

JS還沒有工作,但我的控制器可以在模型狀態回暖,這是從它的響應,那麼該屬性是否正常工作。

這基本上檢查任何一方/任何一方的空格斜線。請注意,這不是一個詳盡的URL驗證器,但它將適用於我當前的項目。如果有人有任何改進,請讓我知道,我會修改這個答案。

我對RegExp不太好,所以沒有從RegularExpressionAttribute派生,也不想從模型中提供錯誤消息。當使用類別作爲URL部分時,該屬性應該建立在更多規則的基礎上。

0

正如Tyrsius所建議的那樣,傳遞類別ID會比較容易,但如果您想傳遞名稱,那麼每次創建鏈接或創建自定義UrlHelper時都會建議使用Url.Encode()。一旦它擊中你的控制器動作,只需執行Url.Decode()即可恢復原始字符串。

更清潔的方法是創建自己的路由處理程序(實現IRouteHandler)爲您完成。

+0

@Tyrsius 只是想知道,像堆棧溢出此頁面上 - 該URL的名稱部分似乎被自動替換/編碼: MVC-3-斜線和 - 空間 - 在路線 怎麼來的這並未不會發生我的 - 即:項目 -/- 實驗室 或類似的東西? – Hoecuspocus