3

我有一个简单的 MVC4 网站,其中包含一个区域(称为“用户”),其中包含一个名为“HomeController”的控制器。

在这个控制器上有两个动作方法:Index 和 Details:

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

    public ActionResult Details(int id)
    {
        return View();
    }
}

“UserAreaRegistration.cs”类如下:

public class UserAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "User";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "User_default",
            "User/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
        );

        context.MapRoute(
            "User_default_no_contoller",
            "User/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
        );
    }
}

还有一个不在名为“HomeController”的区域中的控制器:

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

RouteConfig.cs(针对非区域路由)如下:

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 = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "MvcApplication1.Controllers" }
        );
    }
}

我可以通过任一方式访问“主页”

  • /
  • /家
  • /首页/索引

我还可以在“用户”区域中访问“索引”操作

  • /用户

不能做的是弄清楚如何在名为“详细信息”的区域中使用我的控制器上的另一种方法:

  • /User/Details/5 不起作用,它返回 404

我还注意到,尝试显式访问 Index 方法也没有正确路由:

  • /User/Index 不起作用,它返回 404

我究竟做错了什么?

是否可以在作为区域中“默认”控制器的控制器上使用“索引”以外的任何操作方法?

4

3 回答 3

4

你的第一条路线有太多的默认值,所以它吞噬了一切。

  • URI/User/Details/5匹配User_default路由(控制器:“详细信息”和操作:“5”)。
  • URI/User/Index还匹配第一个路由(控制器:“索引”和默认操作:“索引”)

接近路线的方法是

  • 把最有选择性的路线放在第一位
  • 并仅设置必要的最小默认值
  • 使用路由约束来确保 MVC 路由器不会尝试错误地匹配参数(即,给用于int参数的路由值一个数字约束)
  • 将最通用的路由(即User_default)放在最后(作为不匹配任何更具体路由的 URI 的全部内容)

我会尝试像这样设置区域路线。

context.MapRoute(
        "User_default_no_contoller",
        "User/{action}/{id}",
        defaults: new { controller = "Home", id = UrlParameter.Optional },
        constraints: new { id = @"\d+" },
        namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
    );

context.MapRoute(
        "User_default",
        "User/{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { "MvcApplication1.Areas.User.Controllers" }
    );
于 2012-09-27T03:50:28.993 回答
2

克服这个“多默认”问题的另一个建议是使用命名空间进行分离。

在您的示例中,我的建议是在命名空间 "Areas.User" 中有 Class UserAreaRegistration 。随后,您必须将 HomeController 放在命名空间“Areas.User.Controllers”中的用户区域中

它对我有用并保持代码干净。

彼得

于 2013-12-01T20:58:10.277 回答
0

感谢dbaseman的回答。它帮助我查看了自己的路由问题。我不确定我是否遇到了同样的问题,但这就是它的样子。

        routes.MapRoute("Tag", "Tag/{tag}", new { controller = "Blog", action = "Tag" });
        routes.MapRoute("Category", "Category/{category}", new { controller = "Blog", action = "Category" });
        routes.MapRoute("Post", "Archive/{year}/{month}/{day}/{title}", new {controller = "Blog", action = "Post"});
        routes.MapRoute("Action", "{action}", new { controller = "Blog", action = "Posts" });

当我在任何其他路线上方使用“Action”参数的最后一条路线时,将不会捕获“Action”下方的路线。将它移动到 RouteConfig 的底部允许它上面的所有现在成功命中。

于 2013-01-31T10:42:40.027 回答