完整的URL未获提交(MVC)

时间:2014-03-04 13:15:11

标签: c# asp.net-mvc

我点击SAVE提交表单。

$('#btnSave').click(function () {

    $('#frmFurtherRelevantInformation').submit();

});

我的网址是     http://My.local/TP/FurtherRelevantInformation/655/29

我在Global.asax中有四条路线

#region Further Relevant Information

        routes.MapRoute(
          "FurtherRelevantInformation",  // Route name
          "TP/FurtherRelevantInformation/{SubstanceId}", // URL with parameters
          new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional }  // Parameter defaults
          );

        routes.MapRoute(
         "TPFurtherRelevantInformation", // Route name
         "TP/FurtherRelevantInformation/{SubstanceId}/{JobServiceMapId}", // URL with parameters
         new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional, JobServiceMapId = UrlParameter.Optional }  // Parameter defaults
         );

        routes.MapRoute(
          "FurtherRelevantInformationHttpPost", // Route name
          "TP/FurtherRelevantInformation/{SubstanceId}", // URL with parameters
          new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional },  // Parameter defaults
          new { httpMethod = new HttpMethodConstraint("POST") }
          );

        routes.MapRoute(
         "TPFurtherRelevantInformationHttpPost",  // Route name
         "TP/FurtherRelevantInformation/{SubstanceId}/{JobServiceMapId}",  // URL with parameters
         new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional, JobServiceMapId = UrlParameter.Optional },  // Parameter defaults
         new { httpMethod = new HttpMethodConstraint("POST") }
         );
        #endregion

由于页面可能有第二个参数或可能没有。 但是如果URL有第二个参数,在我的控制器的POST方法中,我没有得到第二个参数,即29。

我检查了Chrome中的请求网址仅显示http://My.local/TP/FurtherRelevantInformation/655

1 个答案:

答案 0 :(得分:1)

问题是,您的所有网址在匹配TP/FurtherRelevantInforamtion/{id}之前都会与TP/FurtherRelevantInformation/{id}/{id}匹配,因此第一条路线始终会被点击。

如果您想优先考虑多参数网址,则需要在单参数路径之前注册

routes.MapRoute(
     "TPFurtherRelevantInformation", // Route name
     "TP/FurtherRelevantInformation/{SubstanceId}/{JobServiceMapId}", // URL with parameters
     new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional, JobServiceMapId = UrlParameter.Optional }  // Parameter defaults
);

routes.MapRoute(
      "FurtherRelevantInformation",  // Route name
      "TP/FurtherRelevantInformation/{SubstanceId}", // URL with parameters
      new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional }  // Parameter defaults
);

routes.MapRoute(
    "TPFurtherRelevantInformationHttpPost",  // Route name
    "TP/FurtherRelevantInformation/{SubstanceId}/{JobServiceMapId}",  // URL with parameters
    new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional, JobServiceMapId = UrlParameter.Optional },  // Parameter defaults
    new { httpMethod = new HttpMethodConstraint("POST") }
);

routes.MapRoute(
    "FurtherRelevantInformationHttpPost", // Route name
    "TP/FurtherRelevantInformation/{SubstanceId}", // URL with parameters
    new { controller = "TP", action = "FurtherRelevantInformation", SubstanceId = UrlParameter.Optional },  // Parameter defaults
    new { httpMethod = new HttpMethodConstraint("POST") }
);