1

我希望我的 ASP.NET MVC 应用程序默认重定向到产品控制器索引操作。所以我将 RouteConfig 更改为

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

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

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

但我仍然收到错误

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

我调试了应用程序并检查了第三条路线的默认部分。但它仍然说

{[controller, Home]} 谁能告诉我为什么?

4

3 回答 3

2

试试这个,它对我有用 确保默认路由位于您列出的路由表的底部。当涉及到 ASP.NET MVC 路由表时,顺序很重要。

 routes.MapRoute(
                  "Default",
                  "{id}",
                  new { controller = "Product", action = "Index", id = UrlParameter.Optional }
              );

还要检查这个工具它会对你有帮助

于 2013-01-28T11:41:15.937 回答
0

试试这个,它会覆盖默认路由并使用你的。

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

        routes.MapRoute(
            "DefaultApi", // Route name
            "{Controller}/{action}/{id}", // URL with parameters
            new { controller = "Product", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }// Parameter defaults
        );
    }
于 2013-11-28T15:58:47.013 回答
0

您还没有为产品控制器添加任何视图。为其添加视图,它将按预期工作。

于 2013-01-28T12:15:16.197 回答