我正在构建一个ASP.Net MVC Web应用程序,它将成为安全设备的入口。该设备支持JSON API。我尝试开发客户端(使用$httpProvider
方法发布和获取数据的angularJS脚本),但却陷入了CORS的问题。如何从ASP.Net MVC控制器发布JSON数据?
我想要做的是:Web应用程序服务器将发布并获取请求,然后将它们作为简单HTML重定向到客户端。
问题是我该如何执行从我的控制器到这个设备的HTTP请求。
这个question有一个答案,不清楚。
要注意我尝试在控制器中使用HTTPWebRequest,但名称空间System.Net.HTTP.WebRequest不会被包含在内,即使它在我的项目的引用中。
编辑: 尝试至今: 这是帐户型号:
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();
}
}
}
这是控制器:
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
}
}
你应该看看[httpclient](https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v = vs.118).aspx) –
@CallumLinington这看起来像它会工作。问题是使用POST和GET与HttpClient导致一些问题,因为post和get是异步的并且控制器是同步的。 –
控制器的动作可以是异步 –