问题是您没有与以字符串结尾的路由匹配的路由。
修改您的路线,类似于:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = 0 },
new { id = "[0-9]" }// Parameter defaults
);
routes.MapRoute(
"Default2", // Route name
"{controller}/{action2}/{sid}", // URL with parameters
new { controller = "Home", action = "Index2", sid = "" } // Parameter defaults
);
并修改您的控制器
public ActionResult Index(int id)
{
ViewData["Title"] = "Home Page";
ViewData["Message"] = "Welcome to ASP.NET MVC! Your id is: "+ id.ToString();
return View();
}
public ActionResult Index2(string sid)
{
ViewData["Title"] = "Home Page 2."+sid.ToString();
ViewData["Message"] = "Welcome to ASP.NET MVC! \"" + sid.ToString() +"\" is an invalid id";
return View("index");
}
现在,当您为 ID 传递一个字符串时,将调用 Index2,您可以做任何您需要做的事情来处理不正确的参数。