[英]Asp.net MVC calling Login web service
我是Asp.Net的新手,我想做的是。 我有一個用於登錄服務的Web API,它返回一個json數據。 Web APi的示例URL
http://localhost:55500/api/Login/submit?username=abc&password=abc123
它返回一個json數據,例如
[{"UserID":0,
"Status":"True",
"Name":"885032-59-6715",
"DepName":"Ajay"}
]
如何在Asp.NET MVC中驗證我的登錄頁面。 如果登錄成功(狀態:真)。 我應該重定向到儀表板,並在我的視圖頁面中顯示json數據。 如果登錄失敗,則應顯示錯誤消息
我的ASP.NET MVC模型校准文件:
namespace LoginPracticeApplication.Models{
public class Login {
[Required(ErrorMessage = "Username is required")] // make the field required
[Display(Name = "username")] // Set the display name of the field
public string username { get; set; }
[Required(ErrorMessage = "Password is required")]
[Display(Name = "password")]
public string password { get; set; }
}}
我的ASP.NET MVC控制器文件:
public ActionResult Index(Login login)
{
if (ModelState.IsValid) // Check the model state for any validation errors
{
string uname = "";
uname = login.username;
string pword = "";
pword = login.password;
string url = "http://localhost:55506/api/Login/submit?username=" + uname + "&password=" + login.password + "";
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage responseMessage = client.GetAsync(url).Result;
var responseData = responseMessage.Content.ReadAsStringAsync().Result;
if (responseData=="true")
{
return View("Show", login); // Return the "Show.cshtml" view if user is valid
}
else
{
ViewBag.Message = "Invalid Username or Password";
return View(); //return the same view with message "Invalid Username or Password"
}
}
else
{
return View();
}
return View();
}
當我嘗試使用上面的代碼登錄時。 它始終顯示“無效的用戶名或密碼”。 因此,在此先感謝您的幫助。 期待成功
我認為問題出在:
var responseData = responseMessage.Content.ReadAsStringAsync().Result;
if (responseData=="true")
{
return View("Show", login); // Return the "Show.cshtml" view if user is valid
}
else
{
ViewBag.Message = "Invalid Username or Password";
return View(); //return the same view with message "Invalid Username or Password"
}
當您使用ReadAsStringAsync()
,響應可能返回您提到的JSON [{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}]
什么意味着測試responseData=="true"
又名。 "[{"UserID":0,"Status":"True","Name":"885032-59-6715","DepName":"Ajay"}]" == "true"
將導致錯誤。
您可以使用responseData.Contains("true")
但我不認為這是最好的方法。
我認為方法是,在ReadAsStringAsync()
,應通過JsonConvert.DeserializeObject<LoginResultModel>(responseData);
將字符串(json)反序列化為對象JsonConvert.DeserializeObject<LoginResultModel>(responseData);
。 JsonConvert在Newtonsoft中。您可以通過Nuget獲得Json。 在LoginResultModel中,應考慮json創建。 我相信會是這樣的:
public class LoginResultModel
{
public int UserID { get; set; }
public bool Status { get; set; }
public string Name { get; set; }
public string DepName { get; set; }
}
在返回數組時,應反序列化到LoginResultModel的列表: JsonConvert.DeserializeObject<List<LoginResultModel>>(responseData);
PS .:您可以進行調試以查看responseData所獲取的數據,並了解其評估為false的原因。
問候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.