简体   繁体   English

ASP.NET MVC-路由不起作用

[英]ASP.NET MVC - Routing doesn't work

I am practicing ASP.NET MVC using vs 2013, and writed a simple project, with one controller and view. 我正在使用vs 2013练习ASP.NET MVC,并编写了一个具有一个控制器和视图的简单项目。

controller code: 控制器代码:

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

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

"MyHome" view Code: “ 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 code: 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 }
              );
        }
    }

when i run and set url line on " http://mySite/Home/ShowMyHome ", it works well. 当我运行并在“ http:// mySite / Home / ShowMyHome ”上设置网址行时,它运行良好。 but when i run and set url line on " http://mySite/Home ", i get a error: 但是当我运行并在“ http:// mySite / Home ”上设置网址行时,出现错误:

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

It looks like the "Home" routing doesn't work. 看起来“本地”路由无效。

Your Home route isn't being reached because when navigating to ~/Home the default route is matched, given that your controller name is HomeController . 由于您的控制器名称为HomeController ,因此导航到~/Home会匹配默认路由,因此无法达到您的Home路由。

If your desired result is to map ~/Home to ShowMyHome() explicitly then you will need to move your custom route above the default one. 如果您想要的结果是将~/Home明确映射到ShowMyHome()则需要将自定义路线移到默认路线上方。

You don't need to define a separate route for each controller actions you create, 9 times out of 10, the default route is fine. 您无需为创建的每个控制器动作定义单独的路由,十分之九(默认路由)就可以了。

The problem isn't routing, it's that ASP.NET can't find your Index.cshtml view. 问题不在于路由,而是ASP.NET找不到您的Index.cshtml视图。 You need to add a view for Index(). 您需要为Index()添加一个视图。

You still have the default route pointing to the 'Index' view and action, which doesn't exist. 您仍然具有指向“索引”视图和操作的默认路由,该默认路由不存在。 Place your custom route before the default route and your problem should be solved. 将自定义路由放在默认路由之前,您的问题应得到解决。

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 }

        );

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

You will need to create a view for Index. 您将需要为Index创建一个视图。 In the Controller, right-click on Index() and choose "Add View". 在控制器中,右键单击Index()并选择“添加视图”。

Then, complete the wizard for how you want that view to behave. 然后,完成向导以了解该视图的行为。

Edit: Based on the answers I'm seeing, I believe there may be a small misunderstanding. 编辑:根据我看到的答案,我相信可能会有一点误会。 You should not need to create a new route for every Controller you make. 您不必为制作的每个Controller创建新的路由。 You should generally use the default route and use Index as your home page. 通常,您应该使用默认路由并将“索引”用作主页。 Custom routes tend to come into play when you need to provide more information to your controller than just /action/id. 当您需要向控制器提供的信息不仅仅限于/ action / id时,自定义路由通常会发挥作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM