NSS
NSS

Reputation: 2022

Passing Object in get Request to WebAPI 2

selectedLocation is always null on the Server

I am using Angular , what is the proper way of passing object to get method

This is Class that is defined on Server, which is being passed as parameter in get request

public class Location
{
   public string LocationName { get; set; }
   public string MFGName { get; set; }
}

This is the method in WebAPI that is being called.

    [HttpGet]
    [ActionName("RenewToken")]
    [AllowAnonymous]
    [ResponseType(typeof(string))]
    public async Task<IHttpActionResult> RenewToken(Location selectedlocation)
    { 

    }

The Captured Request looks like this(In google Chrome Developers Tool)

http://localhost:58146/api/Account/RenewToken?selectedlocation=%7B%22LocationName%22:%22Guad%22,%22MFGName%22:%22Flex%22%7D

What am i doing wrong ?

Upvotes: 0

Views: 1625

Answers (1)

Teliren
Teliren

Reputation: 342

Okay so from what i got from this

Why do we have to specify FromBody and FromUri in ASP.NET Web-API?

when the parameter of the method is a complex type is looks in the body of the request

since you're using GET the data gets put into the uri instead of the body

couple options you could do are

public async Task<IHttpActionResult> RenewToken(string LocationName, string MFGName)
{ 

}

you could always change your verb to a post or something that accepts data in teh body

You might try changing your get in angular to something like

$http({
    method: "GET",
    url: "/api/Account/RenewToken",
    params: {
        LocationName: "Guad",
        MFGName: "Flex"
    }
})

which will parameterize the data

Upvotes: 1

Related Questions