C# ASP.NET MVC中的自定义路由

C# ASP.NET MVC中的自定义路由,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在尝试在ASP.NET MVC 4中设置自定义路由。我希望我的路线可以通过 为了设置此路由,我在RouteConfig.cs中添加了以下内容 routes.MapRoute( name: "HSStudentProfile", url: "high-school/student", defaults: new { controller = "Students", action = "HSStudentProfile" } ); 学生控制器.cs public class St

我正在尝试在ASP.NET MVC 4中设置自定义路由。我希望我的路线可以通过

为了设置此路由,我在RouteConfig.cs中添加了以下内容

routes.MapRoute(
  name: "HSStudentProfile",
  url: "high-school/student",
  defaults: new { controller = "Students", action = "HSStudentProfile" }
);
学生控制器.cs

public class StudentsController : Controller
{
  public ActionResult HSStudentProfile()
  {
    return View("~/Views/Shared/Course/Index.cshtml");
  }
}
如果我访问上面提到的url,我会得到一个403.14错误。如果我更新路由配置以使用

routes.MapRoute(
  name: "HSStudentProfile",
  url: "{controller}/high-school/student",
  defaults: new { controller = "Students", action = "HSStudentProfile" }
);
它可以工作,但是,我必须将URL路径更改为


有没有一种方法可以做到这一点,而不必在我的RouteConfig中使用{controller}?

路径不应该是“高中/学生/{id}”?我怀疑它与其他路径匹配…我的目标是拥有{id}。我如何摆脱/Students/?根据Alexei所说,您的路由在“student”之后没有定义任何内容,因此如果您在那里传递了某个内容(“/1”),它与路由不匹配,因此MVC尝试使用默认处理程序打开它,而控制器不存在,因此它会出错。因此,您的操作方法甚至不接受参数,那么为什么要在URL中传递它呢?