即使 ModelState 包含所有必需的值,DefaultModelBinder 也不绑定模型(始终为 null)
我正在使用示例 ASP.NET MVC 3 应用程序。我正在为我正在从事的项目进行 API 概念验证,方法是使用 HttpWebRequest 发送登录请求。我以 JSON 形式提交数据,并使用内容类型“application/json”。
这是我的方法:
[HttpPost]
[Api]
public bool ApiLogOn(LogOnModel model)
{
if (ModelState.IsValid && model != null)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return true;
}
}
return false;
}
问题是,即使 ModelState
包含我期望看到的所有键和值(例如,字典包含 { "UserName"="username", "Password"=" password", "RememberMe"=true } 这是 LogOnModel 类的三个属性),每次调用该方法时,对象 model
都为 null。
使用“application/x-www-form-urlencoded”时我没有遇到此问题。我还尝试过添加
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
到我的 Application_Start
方法中。
这是我用来向上述方法发送请求的代码:
var credentials = new LogOnModel() { UserName = "username", Password = "password", RememberMe = true };
var result = PostData<LogOnModel, bool>(@"url goes here", credentials);
protected R PostData<T, R>(string uri, T postData)
{
var request = WebRequest.Create(uri) as HttpWebRequest;
request.Headers.Add("X-Requested-With", X_REQUESTED_WITH_HEADER);
request.Accept = "application/json";
request.Method = "POST";
request.CookieContainer = cookieJar;
request.ContentType = "application/json";
var serializerT = new DataContractJsonSerializer(typeof(T));
var serializerR = new DataContractJsonSerializer(typeof(R));
if (postData != null)
{
using (var stream = request.GetRequestStream())
{
serializerT.WriteObject(stream, postData);
}
}
try
{
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
using (var stream = response.GetResponseStream())
{
var result = (R)serializerR.ReadObject(stream);
return result;
}
}
else
{
return default(R);
}
} catch { return default(R); }
}
I am working with the sample ASP.NET MVC 3 application. I am working on a proof of concept for an API for a project I'm working on, by sending log in requests using HttpWebRequest
s. I am submitting data as JSON and am using the Content Type "application/json".
Here is the method I have:
[HttpPost]
[Api]
public bool ApiLogOn(LogOnModel model)
{
if (ModelState.IsValid && model != null)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return true;
}
}
return false;
}
The problem is that even though ModelState
contains all the keys and values that I expect to see (e.g. Dictionary contains { "UserName"="username", "Password"="password", "RememberMe"=true } which are the three properties of the LogOnModel class), the object model
is null every time I call the method.
I do not have this problem when using "application/x-www-form-urlencoded". I have also tried with and without adding
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());
to my Application_Start
method.
Here is the code I am using to send requests to the method above:
var credentials = new LogOnModel() { UserName = "username", Password = "password", RememberMe = true };
var result = PostData<LogOnModel, bool>(@"url goes here", credentials);
protected R PostData<T, R>(string uri, T postData)
{
var request = WebRequest.Create(uri) as HttpWebRequest;
request.Headers.Add("X-Requested-With", X_REQUESTED_WITH_HEADER);
request.Accept = "application/json";
request.Method = "POST";
request.CookieContainer = cookieJar;
request.ContentType = "application/json";
var serializerT = new DataContractJsonSerializer(typeof(T));
var serializerR = new DataContractJsonSerializer(typeof(R));
if (postData != null)
{
using (var stream = request.GetRequestStream())
{
serializerT.WriteObject(stream, postData);
}
}
try
{
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
using (var stream = response.GetResponseStream())
{
var result = (R)serializerR.ReadObject(stream);
return result;
}
}
else
{
return default(R);
}
} catch { return default(R); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,删除 [Api] 属性解决了我的问题。如果有人感兴趣,它来自这个网址: http://mvcapi.codeplex.com/
重点是这样我们就可以使用同时具有视图(对于我们的网页)和 JSON 结果(对于我们的 API 调用)的方法。如果有人对此有任何其他想法(除了明显的 if-this-return-view-else-return-json ),请随时在下面发表评论,我想听听他们的意见。
It turns out that removing the [Api] Attribute solved my problem. If anyone is interested, it came from this url: http://mvcapi.codeplex.com/
The point was such that we could use methods that had both Views (for our web page) and JSON results (for our API calls). If anyone has any other ideas for this (aside from the obvious if-this-return-view-else-return-json) please feel free to comment below, I'd like to hear them.