传入的请求与My sample mvc应用程序中的任何路由都不匹配

时间:2009-11-17 07:37:28

标签: asp.net asp.net-mvc

海派,          我开始了一个新的asp.net mvc应用程序,我按照给出的步骤 http://www.packtpub.com/article/your-first-asp.net-mvc-application

当我按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();
    }

}

1 个答案:

答案 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 = "" });    
 }