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 $httpProvider methods to post and get data) but fallen into the problem of CORS.
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.
The problem is how can I execute a HTTP request from my controller to this device.
This question has one answer which is not clear.
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.
Edit: Attempts so far: This is the Account Model:
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();
}
}
}
And this is the controller:
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<ActionResult> 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
}
}
public async Task<IActionResult> SomeAction()as your controller action or you can doclient.PostAsync().Result.NotImplementedExceptionfor some reason that is beyond me.