Reputation: 17701
I am in a situation where i need to return the single object along with string .. I am very new to web api and just could not be able to figure it out how to return multiple parameters ..
below is the code what i have tried so far ..
public async Task<IHttpActionResult> PostAuthenticationData(long id, string password)
{
Consumer consumer = await db.Consumers.FindAsync(id);
if (consumer == null)
{
return NotFound();
}
if(consumer.ConsumerPassword != password)
{
return BadRequest();
}
ConsumerSessionTokenLog consumerSessionTokenLog = await db.ConsumerSessionTokenLogs.FindAsync(id);
if(consumerSessionTokenLog == null)
{
return NotFound();
}
else
{
string sessionToken = consumerSessionTokenLog.SessionToken;
}
/// here i need to return "sessionToken" and "consumer" object
return Ok(consumer);
}
Could any one please help on this query..
Many thanks in advance
Upvotes: 1
Views: 1881
Reputation: 8099
Joust define a struct to return:
struct MyReturType
{
object obj;
string str;
public MyReturType(object o, string s)
{
obj = o;
str = s;
}
}
And then return that:
public async Task<IHttpActionResult> PostAuthenticationData(long id, string password)
{
Consumer consumer = await db.Consumers.FindAsync(id);
if (consumer == null)
{
return NotFound();
}
if(consumer.ConsumerPassword != password)
{
return BadRequest();
}
ConsumerSessionTokenLog consumerSessionTokenLog = await db.ConsumerSessionTokenLogs.FindAsync(id);
if(consumerSessionTokenLog == null)
{
return NotFound();
}
else
{
string sessionToken = consumerSessionTokenLog.SessionToken;
}
/// here i need to return "sessionToken" and "consumer" object
return Ok(new MyReturType(consumer,sessionToken));
}
Upvotes: 2
Reputation: 27377
Create a class which wraps both values, or you can return an anonymous type like so:
return Ok(new { consumer, sessionToken });
Note that you'll need to hoist sessionToken
out of your if-statement scope.
Upvotes: 3