什么会导致此自定义 XML ModelBinder 无法反序列化我的 XML POST?

发布于 2024-11-23 16:46:29 字数 2587 浏览 0 评论 0原文

模型

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-11-30 16:46:29

我在这里缺少什么?

您缺少在使用流之前重置流:

var inputStream = controllerContext.HttpContext.Request.InputStream;
inputStream.Position = 0;
return serializer.Deserialize(inputStream);

或使用 XmlReader

using (var inputStream = controllerContext.HttpContext.Request.InputStream)
using (var reader = XmlReader.Create(inputStream))
{
    return serializer.Deserialize(reader);
}

What am I missing here?

You are missing to reset the stream before consuming it:

var inputStream = controllerContext.HttpContext.Request.InputStream;
inputStream.Position = 0;
return serializer.Deserialize(inputStream);

or use an XmlReader:

using (var inputStream = controllerContext.HttpContext.Request.InputStream)
using (var reader = XmlReader.Create(inputStream))
{
    return serializer.Deserialize(reader);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文