私はmvcアプリケーションとwebApiアプリケーションを持っています。 webApiを通じてasp.net mvcにサインインする必要があります。私がこのメソッドを使用する場合:Asp.net mvcはhttpClientによってwebApiに接続します
public static string GetToken(LoginModelView login)
{
var pairs = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", login.Login),
new KeyValuePair<string, string> ("Password", login.Password)
};
var content = new FormUrlEncodedContent(pairs);
using (var client = new HttpClient())
{
var response =
client.PostAsync(URL + "/Token", content).Result;
var responseJson = response.Content.ReadAsStringAsync().Result;
var dictionaryToken = JsonConvert.DeserializeObject<Dictionary<string,string>>(responseJson);
string token = dictionaryToken["access_token"];
return token;
}
}
My MVCはビューからモデルをバインドしません。 アクションメソッド
[HttpGet]
public ActionResult SignIn()
{
var login = new LoginModelView();
return View("SignIn",login);
}
[HttpPost]
public ActionResult SignIn(LoginModelView login)
{
string token = DirService.GetToken(login);
Session["token"] = token;
return RedirectToAction("success");
}
それは私がそのメソッドを使用していないとき、それは
@model AdminTool.Models.LoginModelView
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginModelView</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Login, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Login, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Login, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Login" class="btn btn-default" />
</div>
</div>
</div>
}
私の見解だ
public class LoginModelView
{
public string Login { get; set; }
public string Password { get; set; }
}
モデルだ、すべてがOKです。 同じコードが別のアプリケーションで動作します。 誰かが私を助けることができます..
'do not bind model'とはどういう意味ですか?アクションは呼び出されているかヒットしていますか? – Nkosi
私はnullの代わりにオブジェクトが埋め込まれたことを意味します –