[英]How to pass parameters to web api2
Controller signature: 控制器签名:
public class UserAlertsController : ApiController
public IHttpActionResult Post(Guid id, List<long> users)
Route: 路线:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Postman request: 邮递员要求:
http://localhost:50828/api/UserAlerts/4af3fee7-84ae-48fa-9215-45a00c35dbf
Content-Type application/json
[1,2]
With the above setup I receieve the message: 通过以上设置,我收到以下消息:
"Message": "The request is invalid.",
"MessageDetail": "The parameters dictionary contains a null entry
for parameter 'id' of non-nullable type 'System.Guid'"
I also use the standard global.asax 我也使用标准的global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Can any one tell me where I'm going wrong 谁能告诉我我要去哪里错了
If I change my action signature to: 如果我将动作签名更改为:
public IHttpActionResult Post(string id, long[] users)
Then it is bound correctly, but the error is thrown if id is changed to a Guid. 然后将其正确绑定,但是如果将id更改为Guid,则会引发错误。
EDIT 编辑
You made a mistake to pass 4af3fee7-84ae-48fa-9215-45a00c35dbf
that is not a GUID and you should pass 4af3fee7-84ae-48fa-9215-45a00c35dbf6
Pay attention to 6 at the end. 您传递
4af3fee7-84ae-48fa-9215-45a00c35dbf
GUID的4af3fee7-84ae-48fa-9215-45a00c35dbf
犯了一个错误,应该传递4af3fee7-84ae-48fa-9215-45a00c35dbf6
注意最后的6 。
Based on your original Question 根据您的原始问题
The problem is with your route, you should use: "api/{controller}/{id}"
as route url template. 问题出在您的路线上,您应该使用:
"api/{controller}/{id}"
作为路线网址模板。
You are passing guid as a route parameter but your route didn't match the url you are using. 您将guid作为路由参数传递,但是您的路由与您使用的网址不匹配。
Open WebApi.Config
and change your default route to this route: 打开
WebApi.Config
并将默认路由更改为此路由:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Also make sure you have registered your WebApiConfig
in global.asax
in Application_Start
: 还要确保已在
Application_Start
global.asax
中注册了WebApiConfig
:
GlobalConfiguration.Configure(WebApiConfig.Register);
Side Note: 边注:
By default, Web API uses the following rules to bind parameters: 默认情况下,Web API使用以下规则来绑定参数:
If the parameter is a “simple” type, Web API tries to get the value from the URI. 如果参数是“简单”类型,则Web API会尝试从URI中获取值。 Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string.
简单类型包括.NET基本类型(int,bool,double等),以及TimeSpan,DateTime,Guid,十进制和字符串,以及带有可从字符串转换的类型转换器的任何类型。
For complex types, Web API tries to read the value from the message body, using a media-type formatter. 对于复杂类型,Web API尝试使用媒体类型格式化程序从消息正文中读取值。
The GUID value you're passing is in the header. 您要传递的GUID值位于标题中。 You can specify you want to receive the value from the uri via the
FromUri
attribute: 您可以通过
FromUri
属性指定要从uri接收值:
public IHttpActionResult Post([FromUri]Guid id, List<long> users)
Another problem is that you have "api" part in your HTTP URL: 另一个问题是您的HTTP URL中包含“ api”部分:
http://localhost:50828/api/UserAlerts/4af3fee7-84ae-48fa-9215-45a00c35dbf
While your controller is missing it in the declaration. 当您的控制器在声明中丢失它时。 Either remove the "api" part from your url, or add it to your route.
从您的网址中删除“ api”部分,或将其添加到您的路由中。
Either this: 可以这样:
http://localhost:50828/UserAlerts/4af3fee7-84ae-48fa-9215-45a00c35dbf
Or modify your url
under MapRoute
: 或在
MapRoute
下修改您的url
:
url: "api/{controller}/{action}/{id}",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.