2017-08-17 115 views
0

我有一个网站,我们最近做了一些更改,这似乎已经打破了我的一些路由。.Net MVC路由产生循环路由

最初的问题是我在使用下面的表单时不能使用405动词。

using (@Html.BeginForm("Index", "Recommendations", FormMethod.Post)) 
{ 
    <button class="btn btn-large btn-primary" d="btnNext">@ViewBag.TextDisplay</button>   
} 

,这是构建一个URL,使得它在HTML视为Recommendations/,使指数走出(大概是因为这是一个默认的参数,但该指数方法签名被修改,所以它采取了一个可选参数,这似乎是导致该问题。

[HttpPost] 
    public async Task<ActionResult> Index(int? enquiryId) 

为了解决这个问题,我增加了以下我route.config文件

routes.MapRoute(
      name: "DefaultWithIndex", 
      url: "Recommendations/{enquiryId}", 
      defaults: new { controller = "Recommendations", action = "Index", enquiryId= UrlParameter.Optional }); 

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

但日是现在已经有拦截到RecommendationsController内的其他方法的任何调用,并重定向我回索引页面即Recommendations/index

那么,如何改变我的路由配置的副作用,使

recommendations/recommendations/enquiryId=1地图recommendations/indexRecommendations/<other method>Recommendations/<other method>

回答

1

Is enquiryId int我是否假设?如果是这样,你可以限制你的路线只寻找整数。

routes.MapRoute(
     name: "DefaultWithIndex", 
     url: "Recommendations/{enquiryId}", 
     defaults: new { controller = "Recommendations", action = "Index", enquiryId= UrlParameter.Optional }, 
     constraints: new {enquiryId= @"\d+" }); //restrict enquiryId to one or more integers 

将匹配/推荐/ 123而不是/推荐/ MyCustomAction

路由是由默认的贪婪,并会尝试通过对下一个下跌之前,以匹配所有可能的值。

+0

完美,谢谢。 – Matt