Asp.net mvc通过httpClient连接到webApi

问题描述:

我有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; 
      } 
     } 

我的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"); 
     } 

这是模型

public class LoginModelView 
    { 
     public string Login { get; set; } 

     public string Password { get; set; } 
    } 

这是我的看法

@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> 
    } 

当我不使用该方法,一切都很好。 相同的代码在另一个应用程序中起作用。 有人可以帮助我,请..

+0

你是什么意思“不绑定模型”。被称为/被击中的动作是? – Nkosi

+0

我的意思是我得到空,而不是填充对象 –

我想你从数据发布到获取方法(GetToken)。

我注意到,在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

@ВладЛуценко您非常欢迎。 –