更改剃刀中的URL

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

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" }
           );
        }

在家庭控制器中:

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

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

并添加了视图:

但是当我将http:// localhost:61961 / Admin作为url时,找不到该视图.

为什么会这样?我该如何解决?

解决方法:

路由配置是有序的.请将您的管理配置移至RegisterRoutes的顶部.

现在,您将被映射到

Controller: admin
View: index

因为您的默认路由捕获了您的本地主机:{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 }
    );
}
上一篇:c#-ActionLink中的Razor变量


下一篇:c#-在MVC4中的DropDownListFor()上未设置所选值