【发布时间】:2019-12-16 13:37:20
【问题描述】:
我在同一个控制器中有两个 get 动词,所以我试图调用其中一个,但它调用了另一个。我想打电话给GetCoCDropdownOptionsAction,但它总是打电话给GetCoCCallLogHistory
我尝试重命名操作方法,发送 id 但它继续寻找第一个动词(具有 id 参数的那个)
//In the controller ABCController.cs
[HttpGet]
[ActionName("GetCoCCallLogHistory")]
public IHttpActionResult GetCoCCallLog(string code, int id)
{
[HttpGet]
[ActionName("GetCoCDropdownOptionsAction")]
public IHttpActionResult GetCoCDropdownOptions(string code)
//Routing
config.Routes.MapHttpRoute(
name: "AllowingMultipleGetsId",
routeTemplate: "api/{code}/{controller}/{action}/{id}",
defaults: new { controller = "Home", id= RouteParameter.Optional },
constraints: new { code= "[a-zA-Z]+(_[a-zA-Z]+)?" }
);
config.Routes.MapHttpRoute(
name: "AllowingMultipleGets",
routeTemplate: "api/{code}/{controller}/{action}",
defaults: new { controller = "Home" },
constraints: new { code = "[a-zA-Z]+(_[a-zA-Z]+)?" }
);
//In the javascript using angularjs
$http.get('/api/' + $scope.code +
'/ABC/GetCoCDropdownOptionsAction').then(function (response) {
//...
}, function (response) {
//...
});
我收到此错误消息:
{"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Http.IHttpActionResult GetCoCCallLog(System.String, Int32)' in 'xxx.ABCController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
如果您需要更多信息,请提供任何帮助,让我知道。
【问题讨论】:
-
尝试将
int id更改为int? id。 -
您是否打算将 id 设为可选?您已经说过它在路由配置中是可选的,但您通过将其设为 int 使其不是可选的。是哪个?
-
我想调用 GetCoCDropdownOptionsAction 但它正在调用 GetCoCCallLogHistory
-
根据服务器日志记录的请求 url 是什么?
标签: c# ajax rest asp.net-web-api