我如何制作没有ID的路线

时间:2019-04-03 05:47:56

标签: c# asp.net-mvc asp.net-mvc-routing

没有{id}的路由“ Admin”将无法工作

public class PublicAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        { return "Public"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
    }
}

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

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin",
            "admin/{controller}/{action}/{id}",
            new { id = UrlParameter.Optional }
            );
    }
}

我可以使用这个: ../admin/car/edit/3 但是我不能这样: ../ admin / car / edit

如果我删除路由“默认”,我将同时使用这两个路由。 为什么我不能将“ ../admin/car/edit”与“默认”路由一起使用?

1 个答案:

答案 0 :(得分:0)

我不得不更改区域注册的顺序,如下所示:

        App_Start.AutoMapperConfig.Initialize();
        //AreaRegistration.RegisterAllAreas();

        var adminArea = new rentcar2.Areas.Admin.AdminAreaRegistration();
        var adminAreaContext = new AreaRegistrationContext(adminArea.AreaName, RouteTable.Routes);
        adminArea.RegisterArea(adminAreaContext);

        var defaultArea = new rentcar2.Areas.Public.PublicAreaRegistration();
        var defaultAreaContext = new AreaRegistrationContext(defaultArea.AreaName, RouteTable.Routes);
        defaultArea.RegisterArea(defaultAreaContext);