为什么MVC区域路由不能按预期工作?

时间:2022-06-09 08:22:32

I have the following area set up on my site.

我在我的网站上设置了以下区域。

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "AdminDefaultNoAction",
            "Admin/{controller}/{id}",
            new { action = "Home", id = UrlParameter.Optional }, new[] { "DevTest.Areas.Admin.Controllers" }
        );

        context.MapRoute(
            "AdminDefault",
            "Admin/{controller}/{action}/{id}",
            new { action = "Home", id = UrlParameter.Optional }, new[] { "DevTest.Areas.Admin.Controllers" }
        );
    }
}

And the following methods in my controller.

以及我控制器中的以下方法。

public class PlayerController : Controller
{
    public PlayerController()
    {

    }

    [HttpGet]
    public ActionResult Home(Guid id)
    {
        var model = new HomeViewModel();
        // Do Stuff
        return View("Home", model);
    }

    [HttpPost]
    public ActionResult Home(HomeViewModel model)
    {
        // Do Stuff
        return View("Home", model);
    }

    public ActionResult TestMethod(Guid id)
    {
        return Json(new
        {
            Test = "Hi!",
            id = id.ToString()
        });
    }
}

My two home methods work fine. TestMethod works if I hit it with Admin/Player/TestMethod/e0ef4ab3-3fe5-4ea8-8ae1-c16b9defcabe" but fails if I hit it with Admin/Player/TestMethod?id=e0ef4ab3-3fe5-4ea8-8ae1-c16b9defcabe.

我的两个家庭方法工作正常。如果我使用Admin / Player / TestMethod / e0ef4ab3-3fe5-4ea8-8ae1-c16b9defcabe命中它,TestMethod会起作用,但如果我用Admin / Player / TestMethod命中它会失败吗?id = e0ef4ab3-3fe5-4ea8-8ae1-c16b9defcabe。

This is obviously just a demonstrative example. I want to be able to hit some methods in this Controller passing values in (mostly via ajax requests) but the routing is not working as intended.

这显然只是一个示范性的例子。我希望能够在此Controller中传递一些方法(通常是通过ajax请求),但路由不按预期工作。

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

As noted by @Stephen Muecke in the comments, my first route was not specific enough. I had to constrain the id to be a guid for it to work as intended.

正如@Stephen Muecke在评论中指出的那样,我的第一条路线不够具体。我不得不限制id为它按照预期工作的指导。

I did this with a regex as I'm still using Mvc version 4.

我用正则表达式做了这个,因为我还在使用Mvc版本4。

context.MapRoute(
    "AdminDefaultNoAction",
    "Admin/{controller}/{id}",
    new { action = "Home", id = UrlParameter.Optional },
    new { id = @"[a-f0-9-]+" },
    new[] { "DevTest.Areas.Admin.Controllers" }
);

If I had Mvc 5 I could have used:

如果我有Mvc 5我可以使用:

new { guid = new GuidRouteConstraint() }

#1


0  

As noted by @Stephen Muecke in the comments, my first route was not specific enough. I had to constrain the id to be a guid for it to work as intended.

正如@Stephen Muecke在评论中指出的那样,我的第一条路线不够具体。我不得不限制id为它按照预期工作的指导。

I did this with a regex as I'm still using Mvc version 4.

我用正则表达式做了这个,因为我还在使用Mvc版本4。

context.MapRoute(
    "AdminDefaultNoAction",
    "Admin/{controller}/{id}",
    new { action = "Home", id = UrlParameter.Optional },
    new { id = @"[a-f0-9-]+" },
    new[] { "DevTest.Areas.Admin.Controllers" }
);

If I had Mvc 5 I could have used:

如果我有Mvc 5我可以使用:

new { guid = new GuidRouteConstraint() }