2

ASP.NET

[HttpPost]
[Route("apitest")]
public string apitest([FromBody]string str)
{
   Console.Writeline(str); // str is always null
   return null;
}

Angular 2:

var creds = "str='testst'" ;
var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

http.post('http://localhost:18937/apitest', creds, {
            headers: headers
        })
        .map(res => res.json())
        .subscribe(
            (res2) => {
                console.log('subsribe %o', res2)
            }
        );

I also tried creds = {"str":"test"}; without headers JSON.stringify() etc. without success. How do I Post data to ASP.NET?

10
  • 1
    Possible duplicate of Angularjs $http.post, asp.net mvc controller gets null Commented Nov 16, 2015 at 16:11
  • Are you getting any errors or is it hitting the endpoint but str being null? Commented Nov 16, 2015 at 16:12
  • it is hitting the controller but str is null Commented Nov 16, 2015 at 16:17
  • Angular 2 does only allow posting strings btw? At least Typescript says so. Commented Nov 16, 2015 at 16:19
  • Try creds = {"str":"test"} and then JSON.stringify(creds) in the http.post and changing the content-type in the headers to: application/json. Commented Nov 16, 2015 at 16:25

2 Answers 2

0
var creds = {
   str: 'testst'
};

$http.post('http://localhost:18937/apitest', JSON.stringify(creds));

No changes in Web API controller and it should work.

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

3 Comments

I get ExceptionMessage: "No MediaTypeFormatter is available to read an object of type 'String' from content with media type 'text/plain'." ExceptionType: "System.Net.Http.UnsupportedMediaTypeException"
Try add content-type header = "application/json" to ajax request
0

This is probably an issue with the way that ASP.NET and MVC handle data POSTS.

[HttpPost]
public ActionResult Index(int? id)
{
    Stream req = Request.InputStream;
    req.Seek(0, System.IO.SeekOrigin.Begin);
    string json = new StreamReader(req).ReadToEnd();

    InputClass input = null;
    try
    {
        // assuming JSON.net/Newtonsoft library from http://json.codeplex.com/
        input = JsonConvert.DeserializeObject<InputClass>(json)
    }

    catch (Exception ex)
    {
        // Try and handle malformed POST body
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    //do stuff

}

You can refer to my answer here and the referenced links as to a potential cause of the issue. There are quite a few server side web frameworks that inappropriately handle data POSTS and by default doesn't add the data to your request object.

You shouldn't [have to] try and change the behavior of your angular post, and modify headers to pretend your data post is a form post.

2 Comments

I use Web Api and have no Request.InputStream
Correct but you wont be able to get the JSON data from FromBody

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.