什么会导致此自定义 XML ModelBinder 无法反序列化我的 XML POST?
模型
public class SimpleUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public int Role { get; set; }
public bool isActive { get; set; }
public string Groups { get; set; }
}
BinderProvider
public class SimpleUserProvider : IModelBinderProvider
{
public IModelBinder GetBinder(Type modelType)
{
var contentType = HttpContext.Current.Request.ContentType;
if (string.Compare(contentType, @"text/xml", StringComparison.OrdinalIgnoreCase) != 0)
{
return null;
}
return new SimpleUserBinder();
}
}
ModelBinder
public class SimpleUserBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = bindingContext.ModelType;
var serializer = new XmlSerializer(modelType);
var inputStream = controllerContext.HttpContext.Request.InputStream;
return serializer.Deserialize(inputStream);
}
}
Global.asax.cs 中的 Application_Start()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinderProviders.BinderProviders.Add(new SimpleUserProvider());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
操作< /strong>
[HttpPost]
public ActionResult Create(SimpleUser u)
{
//simple output for testing bind
return Content(u.FirstName + ", " + u.LastName + ", " + u.UserName + ", " + u.Role.ToString() + ", " + u.isActive + ", {" + u.Groups + "}", "text/plain");
}
然而,当我发布包含此 XML 的“text/xml”请求时:
<SimpleUser>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<UserName>[email protected]</UserName>
<Role>3</Role>
<isActive>true</isActive>
</SimpleUser>
我得到的只是:
, , , 0, 假, {}
我遵循了 这篇文章,我在这里错过了什么?
The Model
public class SimpleUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public int Role { get; set; }
public bool isActive { get; set; }
public string Groups { get; set; }
}
The BinderProvider
public class SimpleUserProvider : IModelBinderProvider
{
public IModelBinder GetBinder(Type modelType)
{
var contentType = HttpContext.Current.Request.ContentType;
if (string.Compare(contentType, @"text/xml", StringComparison.OrdinalIgnoreCase) != 0)
{
return null;
}
return new SimpleUserBinder();
}
}
The ModelBinder
public class SimpleUserBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var modelType = bindingContext.ModelType;
var serializer = new XmlSerializer(modelType);
var inputStream = controllerContext.HttpContext.Request.InputStream;
return serializer.Deserialize(inputStream);
}
}
The Application_Start() in Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinderProviders.BinderProviders.Add(new SimpleUserProvider());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
The Action
[HttpPost]
public ActionResult Create(SimpleUser u)
{
//simple output for testing bind
return Content(u.FirstName + ", " + u.LastName + ", " + u.UserName + ", " + u.Role.ToString() + ", " + u.isActive + ", {" + u.Groups + "}", "text/plain");
}
Yet when I POST a "text/xml" request containing this XML:
<SimpleUser>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<UserName>[email protected]</UserName>
<Role>3</Role>
<isActive>true</isActive>
</SimpleUser>
All I get back is:
, , , 0, False, {}
I followed this post, What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您缺少在使用流之前重置流:
或使用 XmlReader :
You are missing to reset the stream before consuming it:
or use an XmlReader: