2012-09-19 18 views
1

我想創建一條路徑來訪問我的類別,如下所示:/category/{id}而不是/category/details/{id}ASP.NET MVC路由'/ category/{string}'而不是'/ category/details/{string}'

我已經嘗試了許多可能的例子,像這樣

routes.MapRoute(
    "Category", 
    "category/{id}", 
    new { controller = "category", action = "details", id = UrlParameter.Optional }, 
    new string[] { "Project.Controllers" } 
); 

,但它似乎並沒有工作。有人可以幫我弄這個嗎?

這裏是我的控制器行動

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace Project.Controllers 
{ 
    public class CategoryController : Controller 
    { 
     public ActionResult Details(string id) 
     { 
      return Content(id); 
     } 
    } 
} 

回答

3

我一切正常,我只是把它放在我的默認路線下,需要把新路線以上默認一個。

+0

所以你看到的主要問題是你沒有提供足夠的信息在你的問題。否則,顯然你的路線的順序是錯誤的。 –

0

我只是我的本地機器上試過 本地主機/分類/詳細信息/ 1

routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Category", action = "Details", id = UrlParameter.Optional  } // Parameter defaults 
     ); 



public class CategoryController : Controller 
{ 
    // 
    // GET: /Category/ 

    public ActionResult Details(string id) 
    { 
     return View(); 
    } 

} 

或者你在幹什麼HTTPGET或HttpPost?

+0

'/ category/details/1'也適用於我,我需要的是'/ category/1'的工作。 – sed