当我按F5时,我收到错误“传入的请求与任何路线不匹配”... 任何建议......
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"EmployeeShow", // Route name
"Employee/{firstname}", // URL with parameters
new
{ // Parameter defaults
controller = "EmployeeController",
action = "Show",
firstname = ""
}
);
}
我的控制器
public class EmployeeController : Controller
{
//
// GET: /Employee/
public ActionResult Show(string firstname)
{
if (string.IsNullOrEmpty(firstname))
{
ViewData["ErrorMessage"] = "No firstname provided!";
}
else
{
Employee employee = new Employee
{
FirstName = firstname,
LastName = "Example",
Email = firstname + "@example.com"
};
ViewData["FirstName"] = employee.FirstName;
ViewData["LastName"] = employee.LastName;
ViewData["Email"] = employee.Email;
}
return View();
}
}
答案 0 :(得分:3)
您的RegisterRoutes方法中没有“默认”路由,并且您的第一个请求将与您具有的路由不匹配,因为您已经明确指定了控制器请求....这是基于您的配置的正常和预期行为:< / p>
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("EmployeeShow",
"{controller}/{firstname}", // change this line from "Employee" to {controller}
new {controller = "Employee", action = "Show", firstname = "" });
}