【发布时间】:2016-03-08 10:36:28
【问题描述】:
我正在构建一个 ASP.Net MVC Web 应用程序,它将成为安全设备的门户。本设备支持 JSON API。我尝试开发客户端(使用 $httpProvider 方法发布和获取数据的 angularJS 脚本)但陷入了 CORS 的问题。
我想要做的是:网络应用服务器将发布并获取请求,然后将它们作为简单的 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
-
@CallumLinington 这似乎可行。问题是在 HttpClient 中使用 POST 和 GET 会导致一些问题,因为 post 和 get 是异步的,而控制器是同步的。
-
控制器动作可以是异步的
-
您有两个选择,
public async Task<IActionResult> SomeAction()作为您的控制器操作,或者您可以执行client.PostAsync().Result。 -
@CallumLinington 这引发了
NotImplementedException出于某种我无法理解的原因。
标签: c# asp.net json asp.net-mvc