2
模型什麼會導致此自定義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);
}
}
的的Application_Start()中Global.a sax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinderProviders.BinderProviders.Add(new SimpleUserProvider());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
行動
[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格式的 「文本/ xml」 的要求:
<SimpleUser>
<FirstName>John</FirstName>
<LastName>Smith</LastName>
<UserName>[email protected]</UserName>
<Role>3</Role>
<isActive>true</isActive>
</SimpleUser>
我得到的返回是:
,0,假,{}
我跟着this post,我缺少的是在這裏嗎?
你在哪裏都在,我希望他們支付你足夠。 – BigOmega