2

私は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です。 同じコードが別のアプリケーションで動作します。 誰かが私を助けることができます..

+0

'do not bind model'とはどういう意味ですか?アクションは呼び出されているかヒットしていますか? – Nkosi

+0

私はnullの代わりにオブジェクトが埋め込まれたことを意味します –

答えて

0

私は方法(GetToken)を取得するデータから投稿したと思います。

1

私はLoginModelViewクラスでLoginプロパティを持っていることに気づいて、あなたはそのコードと私のためにその仕事を変更controller.Iで同じオブジェクト名LoginModelView loginを作成します。

モデル: -

  [HttpGet] 
      public ActionResult SignIn() 
      { 
       var loginModelView = new LoginModelView(); 
       return View("SignIn", loginModelView); 
      } 

      [HttpPost] 
      public ActionResult SignIn(LoginModelView loginModelView) 
      { 
       string token = DirService.GetToken(loginModelView); 

       Session["token"] = token; 

       return RedirectToAction("success"); 
      } 

は、その作業を願っています!

ハッピーコーディング!

+0

すごくありがとう!!!!ハッピーコーディング!!! –

+0

@ВладЛууенко大歓迎です。 –

関連する問題