本文介绍了MVC区域的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

区域的文件夹如下:

Areas
    Admin
        Controllers
            UserController
            BranchController
            AdminHomeController

项目目录如下:

Controller
    UserController
        GetAllUsers

地区航线登记

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new { controller = "Branch|AdminHome|User" }
    );
}

项目路线注册

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 },
        namespaces: new string[] { "MyApp.Areas.Admin.Controllers" });
}

在我的路线是这样的: http://mydomain.com/User/GetAllUsers 我得到的资源找不到错误(404)。我加入到UserController的区域后,出现此错误。

When I route like this: http://mydomain.com/User/GetAllUsers I get resource not found error (404). I get this error after adding UserController to Area.

如何解决这个问题?

谢谢...

推荐答案

您已经搞砸了你的控制器命名空间。

You've messed up your controller namespaces.

您主要途径的定义应该是:

Your main route definition should be:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new string[] { "MyApp.Controllers" }
);

和您的管理区登记的路线应该是:

And your Admin area route registration should be:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new { controller = "Branch|AdminHome|User" },
        new[] { "MyApp.Areas.Admin.Controllers" }
    );
}

注意正确的命名空间应该如何使用。

Notice how the correct namespaces should be used.

这篇关于MVC区域的路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:12