【问题标题】:problems with routes in areas地区路线问题
【发布时间】:2014-07-19 13:36:15
【问题描述】:

我有一个叫做广告的区域。此区域为两条路线规则。但不幸的是,一次只有一个路由规则有效。通常第一条规则下面的其他规则不起作用。但如果我交换规则,首先出现的规则会起作用。

我会粘贴规则和调用规则的 html 链接。

来自 AdvertAreaRegistration.cs 的代码 sn-p

       public override void RegisterArea(AreaRegistrationContext context)
        {


               context.MapRoute(
       "Advert_second",
       "Advert/{controller}/{action}",
       new { controller = "AdvertAdmin", action = "Index" },
       namespaces: new string[] { "LiveChatPrototype.Mvc.Areas.Advert.Controllers" }
   );


        context.MapRoute(
           name: "Advert_default",
            url: "Advert/{id}/{advertid}",
            defaults: new {  controller = "Advertisement", action = "Index", id =UrlParameter.Optional, advertid = UrlParameter.Optional },
            namespaces: new string[] { "LiveChatPrototype.Mvc.Areas.Advert.Controllers" }
        );


}

我用来调用我的规则的 html 链接

这是第一条规则。

这是第二条规则。

如果规则在先,任何一个链接都可以工作。

请问我怎样才能让这两个规则同时起作用。

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-mvc-routing


    【解决方案1】:

    目前两条路段是相同的

    “广告/{controller}/{action}”

    “广告/{id}/{advertid}”

    两者都有两个动态段,所以asp.net mvc无法区分两者,匹配第一个。

    但似乎 "Advert/{id}/{advertid}" idadvertid 中的段是整数.然后您可以在路由中添加正则表达式约束以区分它们。 喜欢

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            name: "Advert_default",
            url: "Advert/{id}/{advertid}",
            defaults: new {  controller = "Advertisement", action = "Index" },
            constraints: new { id = @"\d+", advertid = @"\d+" }
            namespaces: new string[] { "LiveChatPrototype.Mvc.Areas.Advert.Controllers" }
        );
        context.MapRoute(
            "Advert_second",
            "Advert/{controller}/{action}",
            new { controller = "AdvertAdmin", action = "Index" },
            namespaces: new string[] { "LiveChatPrototype.Mvc.Areas.Advert.Controllers" }
        );
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 2015-10-11
      • 1970-01-01
      相关资源
      最近更新 更多