C# 如何为特定的路由Asp.NETMVC构造我的操作方法

C# 如何为特定的路由Asp.NETMVC构造我的操作方法,c#,asp.net-mvc,routes,url-routing,asp.net-mvc-routing,C#,Asp.net Mvc,Routes,Url Routing,Asp.net Mvc Routing,我有两条路线,我正在尝试创建,以便像这样使用 www.mysite.com/Rate/Student/Event/123 www.mysite.com/Rate/Teacher/Event/1234 如何构造动作方法 这是我的费率控制器中的内容 public ActionResult Student(int id) { return View(); } public ActionResult Teacher(int id) { return View(); } 您已经设置了与

我有两条路线,我正在尝试创建,以便像这样使用

www.mysite.com/Rate/Student/Event/123

www.mysite.com/Rate/Teacher/Event/1234

如何构造动作方法

这是我的费率控制器中的内容

public ActionResult Student(int id)
{
    return View();
}

public ActionResult Teacher(int id)
{
    return View();
}

您已经设置了与URL匹配的路由,但是您没有告诉MVC将请求发送到哪里。MapRoute通过使用路由值工作,路由值可以默认为特定值,也可以通过URL传递。但是,你两个都不做

注意:MVC中需要
控制器
操作
路由值

选项1:添加默认管线值。 选项2:通过URL传递路由值。
您已经设置了与URL匹配的路由,但是您没有告诉MVC将请求发送到哪里。MapRoute通过使用路由值工作,路由值可以默认为特定值,也可以通过URL传递。但是,你两个都不做

注意:MVC中需要
控制器
操作
路由值

选项1:添加默认管线值。 选项2:通过URL传递路由值。
public ActionResult Student(int id)
{
    return View();
}

public ActionResult Teacher(int id)
{
    return View();
}
    routes.MapRoute(
        name: "Rate",
        url: "Rate/Student/Event/{id}",
        defaults: new { controller = "Rate", action = "Student" }
    );

    routes.MapRoute(
        name: "Rate",
        url: "Rate/Teacher/Event/{id}",
        defaults: new { controller = "Rate", action = "Teacher" }
    );
    routes.MapRoute(
        name: "Rate",
        url: "{controller}/{action}/Event/{id}"
    );