0

Below is my Controller Class:

using Pay.Models;
using System.Web.Http;
using Pay.General;

namespace Pay.Controllers
{
    [RoutePrefix("Account")]
    public class AccountsController : ApiController     
    {

        [Route("Login")]
        [HttpPost]   
        public UserSession Login([FromBody]LoginUser loginUser)
        {
            if (loginUser.email != null && loginUser.password !=null && loginUser.token !=null) { }
            byte[] mSessionID = new Session().getmSessionID();
            return new UserSession
            {
                SessionID = mSessionID
            };
        }
    }
}

The Model class LoginUser is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Pay.Models
{
    public class LoginUser
    {
        public string email { get; set; }
        public string password { get; set; }
        public byte[] token { get; set; }
    }
}

But When I call the Login function, the LoginUser object is set to null. Please advise on how to set JSON objects into Controller class.

5
  • try to change FromBody to FromURI Commented Aug 17, 2016 at 7:26
  • can you share your caller method ? Commented Aug 17, 2016 at 7:32
  • refer this link it might help to you stackoverflow.com/questions/5022958/… Commented Aug 17, 2016 at 7:34
  • I am able to input values from JSON object into Model class(LoginUSer.java) except for Token.. Token remains as null, both other values are getting mapped to the LoginUSer class Commented Aug 18, 2016 at 1:47
  • My colleague researched well, and she was able to find a proper link. Commented Aug 21, 2016 at 23:49

1 Answer 1

0

I got help from my colleague, and she was able to find a proper link.

https://weblog.west-wind.com/posts/2013/dec/13/accepting-raw-request-body-content-with-aspnet-web-api....

The code changes I made to fix the issue are

public async Task Login() { string input = await Request.Content.ReadAsStringAsync(); dynamic data = Newtonsoft.Json.Linq.JObject.Parse(input); }

(input -returns as a string in in JSON format) (data - returns the JSON Object)

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

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.