3

RouteConfig.cs我的asp.net mvc4 应用程序中的文件有问题:

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: "Admin",
               url: "Admin",
               defaults: new { controller = "Home", action = "Administration" }
           );
        }

home控制器中:

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

        public ActionResult Administration()
        {
            return View();
        }

并添加了视图:

意见

但是当我把http://localhost:61961/Admin它作为 url 时,找不到视图。

为什么会发生这种情况?我该如何解决?

4

1 回答 1

3

路由配置是有序的。请将您的管理配置移动到 RegisterRoutes 的顶部。

现在你将被映射到

控制器:管理员
视图:索引

当您的默认路由捕获您的localhost:{port}/admin

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

    routes.MapRoute(
       name: "Admin",
       url: "Admin",
       defaults: new { controller = "Home", action = "Administration" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
于 2013-06-17T09:35:52.763 回答