简体   繁体   中英

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

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.

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.

Based on your original Question

The problem is with your route, you should use: "api/{controller}/{id}" as route url template.

You are passing guid as a route parameter but your route didn't match the url you are using.

Open WebApi.Config and change your default route to this route:

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 :

GlobalConfiguration.Configure(WebApiConfig.Register);

Side Note:

By default, Web API uses the following rules to bind parameters:

  • If the parameter is a “simple” type, Web API tries to get the value from the 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.

  • For complex types, Web API tries to read the value from the message body, using a media-type formatter.

The GUID value you're passing is in the header. You can specify you want to receive the value from the uri via the FromUri attribute:

public IHttpActionResult Post([FromUri]Guid id, List<long> users)

Another problem is that you have "api" part in your HTTP URL:

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.

Either this:

http://localhost:50828/UserAlerts/4af3fee7-84ae-48fa-9215-45a00c35dbf

Or modify your url under MapRoute :

url: "api/{controller}/{action}/{id}",

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM