我希望我的ASP.NET MVC应用程序默认重定向到Product controller index action。所以我将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]}
谁能告诉我为什么?
答案 0 :(得分:2)
试试这个,它对我有用 确保默认路由位于列出的路由表的 BOTTOM 。对于ASP.NET MVC路由表,顺序很重要。
routes.MapRoute(
"Default",
"{id}",
new { controller = "Product", action = "Index", id = UrlParameter.Optional }
);
还要检查this tool它对您有帮助
答案 1 :(得分:0)
您尚未为Product Controller添加任何视图。为它添加视图,它将按预期工作。
答案 2 :(得分: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
);
}