0

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

http://www.mydomain.com/high-school/student/1

为了尝试设置此路线,我在 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 路径更改为

http://www.mydomain.com/Students/high-school/student/1

有没有办法做到这一点而不必在我的 RouteConfig 中有 {controller}?

4

0 回答 0