我的 Web API 路由有什么问题?
What is wrong with my Web API routing?
我有一个带有以下两个动作签名的控制器。我正在使用常规路由与属性路由,因为我一直在尝试任何方法来完成这项工作,并且常规删除了 2 个问题中的 1 个。
[HttpGet]
[ResponseType(typeof(IEnumerable<EmployeeBindingModel>))]
public async Task<IHttpActionResult> ReadAll()
当我使用 URL 从 Postman 调用它时:http://localhost:53093/api/Employees/ReadAll
我得到以下 JSON 返回:
{
"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.Threading.Tasks.Task`1[System.Web.Http.IHttpActionResult] Read(Int32)' in 'AcmeSoft.WebApi.Controllers.EmployeesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
另一个动作是:
[HttpGet]
[ResponseType(typeof(EmployeeBindingModel))]
public async Task<IHttpActionResult> Read(int id)
当我用这个 URL 调用它时:http://localhost:53093/api/Employees/Read/5
我得到了预期的员工 JSON 记录。
我的路由设置如下WebApiConfig
:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional});
我一直使用这个标准样板,今天是我第一次得到这个错误。为什么没有参数的动作在地球上会期望一个? ReadAll
从来没有参数,所以它甚至不是什么鬼。我已经删除了所有 ASP.NET 个临时文件,没有任何变化。
在 WebApiConfig 中进行更改:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional});
也在routeTemplate中使用action
我有一个带有以下两个动作签名的控制器。我正在使用常规路由与属性路由,因为我一直在尝试任何方法来完成这项工作,并且常规删除了 2 个问题中的 1 个。
[HttpGet]
[ResponseType(typeof(IEnumerable<EmployeeBindingModel>))]
public async Task<IHttpActionResult> ReadAll()
当我使用 URL 从 Postman 调用它时:http://localhost:53093/api/Employees/ReadAll
我得到以下 JSON 返回:
{
"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.Threading.Tasks.Task`1[System.Web.Http.IHttpActionResult] Read(Int32)' in 'AcmeSoft.WebApi.Controllers.EmployeesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
}
另一个动作是:
[HttpGet]
[ResponseType(typeof(EmployeeBindingModel))]
public async Task<IHttpActionResult> Read(int id)
当我用这个 URL 调用它时:http://localhost:53093/api/Employees/Read/5
我得到了预期的员工 JSON 记录。
我的路由设置如下WebApiConfig
:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional});
我一直使用这个标准样板,今天是我第一次得到这个错误。为什么没有参数的动作在地球上会期望一个? ReadAll
从来没有参数,所以它甚至不是什么鬼。我已经删除了所有 ASP.NET 个临时文件,没有任何变化。
在 WebApiConfig 中进行更改:
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new {id = RouteParameter.Optional});
也在routeTemplate中使用action