1

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks rob for the help :)
2

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));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.