ASP.NET自定义路由,用于简单需求管理工具

时间:2010-05-11 06:48:46

标签: asp.net-mvc routes

我正在实施一个非常简单的需求管理工具。

我希望网址看起来像这样:

显示“Project One”的主页:
http://projectmanager/Project/Project%20One

显示“Project One”正在处理的要求列表
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

由需求控制器处理。

甚至可能吗?

1 个答案:

答案 0 :(得分: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取决于你想要的其他路线。

编辑:顺便说一下:应该在默认路线之前添加此路线(如果您仍然从应用模板中获取此路线)。