Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 找不到资源。mvc中的RouteConfig_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

Asp.net mvc 找不到资源。mvc中的RouteConfig

Asp.net mvc 找不到资源。mvc中的RouteConfig,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,详情请参阅: RouteConfig类: public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default"

详情请参阅:

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: "Templates",
                url: "templates/{controller}/{template}",
                defaults: new { action = "Template" }
                );
        }
    }
TeamsController:

  public class TeamsController : Controller
    {
        public ActionResult Template(string template)
        {
            switch (template.ToLower())
            {
                case "list":
                    return PartialView(Url.Content("~/Views/Teams/List.cshtml"));
                case "add":
                    return PartialView(Url.Content("~/Views/Teams/Add.cshtml"));
                case "delete":
                    return PartialView(Url.Content("~/Views/Teams/Delete.cshtml"));
                case "edit":
                    return PartialView(Url.Content("~/Views/Teams/Edit.cshtml"));
                case "detail":
                    return PartialView(Url.Content("~/Views/Teams/Detail.cshtml"));
                default:
                    throw new Exception("template not known");
            }
        }
    }
url请求:http://localhost:1533/templates/teams/add

错误:“/”应用程序中的服务器错误

找不到资源


发生此错误的原因?

请尝试在RouteConfig.cs文件中重新排列路由,如图所示:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Templates",                          //move custom routes above default routes
            url: "templates/{controller}/{template}",
            defaults: new { action = "Template" }
            );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional}
            );
    }
}
我在这里为您的问题提供一个小的解释,以便您能够轻松理解,当我们运行mvc应用程序时,会在global.asax文件中生成一个路由表,您在其中注册路由,因此根据您的路由,默认路由将首先注册,默认路由将优先于自定义路由,因此始终建议将自定义路由置于默认路由之上,如我的回答

这是一篇关于MVC路由中常见错误的好文章

将默认管线移动到模板管线之后