2010-05-11 53 views
1

我正在實現一個非常簡單的需求管理工具。簡單需求管理工具的ASP.NET自定義路由

我想要的URL看起來像這樣:

顯示主頁「一號工程」:
http://projectmanager/Project/Project%20One

顯示的要求列表正爲「一號工程」
http://projectmanager/Project/Project%20One/Requirements

顯示「Project One」的要求1
http://projectmanager/Project/Project%20One/Requirement/1

我怎麼能建立路由,使

http://projectmanager/Project/Project%20One 

由項目控制器

http://projectmanager/Project/Project%20One/Requirements 
and 
http://projectmanager/Project/Project%20One/Requirements/1 

被要求控制器處理處理。

這有可能嗎?

回答

0

我認爲這是。

選項之一:

控制器:

public class ProjectController : Controller 
{ 
    public ActionResult Project(string projectName) 
    { 
     return Content("Project: " + projectName); 
    } 
} 

public class RequirementsController : Controller 
{ 
    public ActionResult Requirements(string projectName) 
    { 
     return Content("Requirements: " + projectName); 
    } 

    public ActionResult Requirement(string projectName, int id) 
    { 
     return Content("Requirement: " + projectName + " - " + id); 
    } 
} 

路線報名:

 routes.MapRoute(null, 
         "Project/{projectName}", 
         new {controller = "Project", action = "Project"}); 
     routes.MapRoute(null, 
         "Project/{projectName}/Requirements", 
         new { controller = "Requirements", action = "Requirements" }); 
     routes.MapRoute(null, 
         "Project/{projectName}/Requirement/{id}", 
         new { controller = "Requirements", action = "Requirement" }); 

的ID取決於什麼其他途徑做你想要的。

編輯:順便說一下:這條路線應該添加在您的默認路線之前(如果您仍然從應用程序模板中獲得)。