我有以下课程
public class RegisterExternalBindingModel
{
public string UserName { get; set; }
public string ExternalAccessToken { get; set; }
public string Provider { get; set; }
}
以下WebAPI方法
[AllowAnonymous]
[Route("LoginExternal")]
[HttpGet]
public async Task<IHttpActionResult> LoginExternal(RegisterExternalBindingModel model)
我使用以下代码调用Web API方法
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, new Uri(ConfigurationSettings.AppExternalAccessTokenUrl));
var requestParam = JsonConvert.SerializeObject(new { UserName = "Dan", ExternalAccessToken = googleAccessToken, Provider = "google" });
request.Content = new HttpStringContent(Uri.EscapeDataString(requestParam));
if (request.Content.Headers.ContainsKey("Content-Type"))
{
request.Content.Headers.Remove("Content-Type");
}
request.Content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendRequestAsync(request);
但是,当Web API方法LoginExternal执行时,会创建参数对象本身但单个值为null。有人能告诉我这里我做错了吗?
答案 0 :(得分:0)
从它的外观看,你需要强制WebAPI从消息体中读取模型:
public async Task<IHttpActionResult> LoginExternal([FromBody] RegisterExternalBindingModel model)
如果这没有做到这一点,你还应该用HttpPost动词标记它:
[HttpPost]
public async Task<IHttpActionResult> LoginExternal([FromBody] RegisterExternalBindingModel model)