0

I'm trying pass a JSON object to a method in asp.net mvc 4 but it always return null.

How could I do this ?

trying

Asp.Net Method

    [WebMethod]
    public JsonResult doLoginApp(string model){                     
        jsonResposta.Add("status", "1");
        jsonResposta.Add("email", model);
        return Json(jsonResposta);
    }

JSON Object

{"User": {"email":"[email protected]", "password":"xxxxx"}};

Return

{
    "status": "1",
    "email": null
}

URL

http://localhost:14807/User/doLoginApp

Edit Post

Model

public class UserJsonModel{

    public long id                  { get; set; }         
    public String nome              {   get;set;}            
    public String email             {get;set;}    
    public String senha             {get;set;}   
    public int status               { get; set; } //1 ativo, 2 inativo, 0 aguardando
    public int tipo                 { get; set; } //1 painel, 2 aplicativos
    public String imagem            { get; set; }
    public int loginBy              { get; set; } //0 app, 1 facebook
    public UserJsonModel() { }

}

Asp.NET method

    [WebMethod]
    [HttpPost]
    public JsonResult doLoginApp(UserJsonModel model){
        jsonResposta.Add("status", "1");
        jsonResposta.Add("email", model.email);
        return Json(jsonResposta);
    }
3
  • To start, your JSON object isn't just a string. You need a C# model that matches your JSON object. http://json2csharp.com/ would be helpful for figuring out what that model might look like. Two other things that could also cause problems are your RouteConfig.cs and whatever is making the request. Are you using AJAX? Commented Nov 10, 2016 at 14:56
  • With Ajax it works however in this post I'm trying send the Json object from an Android app. Commented Nov 10, 2016 at 15:02
  • Ok I gotcha, I'm not well versed with Java so I won't be much help there but creating the C# model is a good place to start. Since it's returning something even though it's not what you expected that means that routing is finding the method you want to execute. Commented Nov 10, 2016 at 15:05

1 Answer 1

2

First Create a Model that map your json object and define in your method how you will pass the object (ex from body)

[WebMethod]
[HttpPost]
public JsonResult doLoginApp([FromBody] MyModel model){                     
    jsonResposta.Add("status", "1");
    jsonResposta.Add("email", model);
    return Json(jsonResposta);
}

And search in java how to make HttpRequest Post and sent data in body, and it will work.

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

2 Comments

I edited the post with a Model, but the email still returning null. There's any way to receive the JSON Object directly without use [FromBody] ?
the problem not in this method but in How you call it from Java app, stackoverflow.com/questions/32700391/…

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.