3
我從控制檯應用程序調用MVC3控制器操作(JsonResult)。爲了進行呼叫,我使用了System.Net.Http.HttpClient和PostAsJsonAsync方法。如何使用PostAsJsonAsync調用MVC3控制器操作
在PostAsJsonAsync方法中,我傳遞一個小的poco實例。
這一切都很有用,除了一件事。
我的問題是,我不能在調用MVC3控制器動作時拿起poco實例。誰能告訴我如何做到這一點?
下面的代碼:
在控制檯應用程序調用代碼:
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:50285/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var user = new HashedUser { UserName = "userName", PasswordHash = "abcdef".GetHashCode() };
var response = client.PostAsJsonAsync("app/test", user).Result;
var txt = response.Content.ReadAsAsync<string>().Result;
在MVC3項目接收代碼
public class AppController : Controller
{
public JsonResult Test(HashedUser json)
{
// Problem: json is always a new instance
// of HashedUser and not the one passed in
// from the call to this controller action.
return Json("qqq ppp 777 888" + json.UserName);
}
}
public class HashedUser
{
public string UserName { get; set; }
public int PasswordHash { get; set; }
}