我的控制器中有以下内容:
public ActionResult Details(string name)
{
Student student = db.Students.FirstOrDefault(x => x.FirstName == name);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
// GET: /Student/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
RouteConfig.cs:
// /Student/Details/Id
routes.MapRoute(
name: "StudentDetailsId",
url: "{Student}/{Action}/{id}",
defaults: new { controller = "Student", action = "Details", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
// /Student/Details/Name
routes.MapRoute(
name: "StudentDetailsName",
url: "{Student}/{Details}/{name}",
defaults: new { controller = "Student", action = "Details" }
);
// /Student/Name
routes.MapRoute(
name: "StudentName",
url: "{Student}/{name}",
defaults: new { controller = "Student", action = "Details" }
);
因此,我几乎希望拥有相同的动作名称,但使用 id:int 或字符串获取。
但是我得到以下信息:
The current request for action 'Details' on controller type 'StudentController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Details(System.String) on type MySuperAwesomeMVCApp.Controllers.StudentController
System.Web.Mvc.ActionResult Details(System.Nullable`1[System.Int32]) on type MySuperAwesomeMVCApp.Controllers.StudentController