斜線和空格我有我的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
它在調試時從不會到達索引操作。
我的問題是,我該如何做這項工作,或者當創建類別名稱以防止發生這種情況時,我必須進行一些輸入驗證?
您是否考慮過傳遞類別ID? – Tyrsius
您確定需要對輸入的值進行修剪。不只是檢查空格......有些字符不會顯示在URL(「Ñ」或其他非英文字符)中......請記住還有其他語言。所以,爲了避免這一切...我建議你去爲類別ID。 – Romias