title: How to post JSON data from ASP.Net MVC controller?
body: <p>I am building an ASP.Net MVC web application which will be a portal for a secured device. This device supports JSON API. I tried developing client side (angularJS scripts with <code>$httpProvider</code> methods to post and get data) but fallen into the problem of CORS.</p>
  
  <p>What I want to do is: The web app server will post and get the requests then they will be redirected as simple HTML to the client.</p>
  
  <p>The problem is how can I execute a HTTP request from my controller to this device.</p>
  
  <p>This <a href="https://stackoverflow.com/questions/24206049/post-json-data-from-asp-net-mvc-controller-to-another-url">question</a> has one answer which is not clear.</p>
  
  <p>To note I tried using HTTPWebRequest in the controller but the namespace System.Net.HTTP.WebRequest would not be included even though it is in the references of my project.</p>
  
  <p><strong>Edit:</strong>
  <strong>Attempts so far:</strong>
  This is the Account Model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Data.Entity;
  using System.Globalization;
  using System.Web.Security;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using System.Threading.Tasks;
  
  namespace Project1.Models
  {
      public class LoginModel
      {
          [Required]
          [Display(Name = "User name")]
          public string UserName { get; set; }
  
          [Required]
          [DataType(DataType.Password)]
          [Display(Name = "Password")]
          public string Password { get; set; }
  
  
  
  
          internal System.Web.Mvc.ActionResult PostJson(LoginModel model, StringContent query)
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  
  <p>And this is the controller:</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using System.Transactions;
  using System.Web;
  using System.Web.Mvc;
  using Newtonsoft;
  using System.Web.Security;
  using DotNetOpenAuth.AspNet;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using Microsoft.Web.WebPages.OAuth;
  using WebMatrix.WebData;
  using Project1.Models;
  
  namespace Project1.Controllers
  {
      [Authorize]
      public class AccountController : Controller
      {
          public async Task&lt;ActionResult&gt; PostJson(LoginModel model, StringContent data)
          {
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri("http://localhost:1532/Account/Login");
                  HttpResponseMessage response = await client.PostAsync("http://192.168.30.1/jsonrpc", data);
                  if (response.IsSuccessStatusCode)
                  {
                      Console.Write(response.ToString());
  
                  }
                  Console.Write(response.ToString());
                  return View(model);
  
              }
          }
  
          //
          // GET: /Account/Login
  
          [AllowAnonymous]
          public ActionResult Login(string returnUrl)
          {
              ViewBag.ReturnUrl = returnUrl;
              return View();
          }
  
          //
          // POST: /Account/Login
  
          [HttpPost]
          [AllowAnonymous]
          [ValidateAntiForgeryToken]
          public ActionResult Login(LoginModel model, string returnUrl)
          {
              if (ModelState.IsValid)
              {
                  var data ="{\"params\" : [{\"url\" : \"sys/login/user\",\"data\" : [{\"passwd\" :" + model.Password +",​\"user\" :" + model.UserName + "}]}],\"session\" : 1,\"id\" : 1,\"method\" : \"exec\"}";
                  StringContent query = new StringContent(data);
                  return (model.PostJson(model,query));
              }
  
              // If we got this far, something failed, redisplay form
              ModelState.AddModelError("", "The user name or password provided is incorrect.");
              return View(model);
          }
  
  
          #region Helpers
          private ActionResult RedirectToLocal(string returnUrl)
          {
              if (Url.IsLocalUrl(returnUrl))
              {
                  return Redirect(returnUrl);
              }
              else
              {
                  return RedirectToAction("Index", "Home");
              }
          }
          #endregion
      }
  }
  </code></pre>