我在asp.net mvc视图中有一个登录/索引视图
@{
ViewData["Title"] = "Index or login page";
}
<div class="text-center">
<label>Email Address</label>
<input type="email" id="txtemail" placeholder="Enter your email" />
<br/>
<label>Password</label>
<input type="password" id="txtpass" placeholder="Enter your email" />
<button type="button" id="btnLogin">Login</button>
</div>
我的模型课中有以下数据
namespace test.Models
{
public class Login_Model
{
public string email { get; set; } //you get to see the user email.
public string name { get; set; } //user name
public Product[] products { get; set; } //list of products
public string result { get; set; } // success/unsuccess
public string message { get; set; }
}
public class Product
{
public string billingcycle { get; set; }
public string nextduedate { get; set; }
public int pid { get; set; }
public string product_name { get; set; }
public string product_package { get; set; }
public string regdate { get; set; }
public string status { get; set; }
public string username { get; set; }
}
}
我面临的问题是,在该模型类中,我有两个参数“ uemail”和“ upwd”(在我的模型类中未公开),用于登录并生成结果和产品详细信息。但是,当我在控制器中将Login_Model类作为[HttpPost]方法的参数传递时,无法将“ uemail”和“ upwd”与用户输入进行比较。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using test.Models;
using RestSharp;
namespace test.Controllers
{
public class HomeController : Controller
{
private string baseUrl = "https://webservice.sample.com/";
private RestClient client;
private RestRequest Request;
private IRestResponse response;
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Index(Login_Model login)
{
client = new RestClient(baseUrl);
Request = new RestRequest("login.php", Method.POST);
return RedirectToAction("Privacy");
}
public IActionResult Privacy()
{
return View();
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
}
什么是在asp.net mvc中获取字符串json.result ==“ success”来将我的索引视图重定向到隐私视图的最佳方法。
是的,您可以将HttpWebRequest类用于通话休息APi
string url = "https://api.xxxxx.com/v1/login/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers.Add("API-key", "your-api-key-if-any");
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)";
request.Accept = "/";
request.UseDefaultCredentials = true;
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
HttpWebResponse resp = request.GetResponse() as HttpWebResponse;
using (var streamReader = new StreamReader(resp.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}