ASP.NET MVC - 路由不起作用

时间:2016-03-14 14:29:47

标签: c# asp.net-mvc

我正在使用vs 2013练习ASP.NET MVC,并使用一个控制器和视图编写了一个简单的项目。

控制器代码:

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult ShowMyHome()
    {
        return View("MyHome");
    }
} 

"是MyHome"查看代码:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Hello World</title>
</head>
<body>
    <div> 
        Welcome my first MVC page
    </div>
</body>
</html>

RouteConfig代码:

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

            );

            routes.MapRoute(
              name: "Home",
              url: "Home",
              defaults: new { controller = "Home", action = "ShowMyHome", id = UrlParameter.Optional }
              );
        }
    }

当我在&#34; http://mySite/Home/ShowMyHome&#34;上运行并设置网址时,它运行良好。 但当我在&#34; http://mySite/Home&#34;上运行并设置网址时,我收到错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

它看起来像&#34; Home&#34;路由不起作用。

5 个答案:

答案 0 :(得分:3)

由于您的控制器名称为Home,导航到~/Home默认路由时,未找到您的HomeController路由。

如果您希望的结果是明确地将~/Home映射到ShowMyHome(),那么您需要将自定义路线移到默认路线之上。

您不需要为您创建的每个控制器操作定义单独的路由,10次中有9次,默认路由没问题。

答案 1 :(得分:2)

问题不在于路由,因此ASP.NET无法找到您的Index.cshtml视图。您需要为Index()添加一个视图。

答案 2 :(得分:2)

您仍然拥有指向“索引”视图和操作的默认路由,该路径不存在。将您的自定义路线放在默认路线之前,您的问题应该得到解决。

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
          name: "Home",
          url: "Home",
          defaults: new { controller = "Home", action = "ShowMyHome", id = UrlParameter.Optional }
          );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

        );

    }
}

答案 3 :(得分:1)

  routes.MapRoute(
          name: "Home",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "ShowMyHome", id =UrlParameter.Optional }
          );

答案 4 :(得分:0)

您需要为Index创建一个视图。在Controller中,右键单击Index()并选择&#34; Add View&#34;。

然后,完成向导,了解您希望该视图的行为方式。

编辑:根据我所看到的答案,我相信可能会有一个小小的误解。您不需要为您创建的每个Controller创建新路由。您通常应使用默认路由并使用Index作为主页。当您需要向控制器提供更多信息而不仅仅是/ action / id时,自定义路线往往会发挥作用。

相关问题