gpt4 book ai didi

asp.net-mvc - 是什么导致 ASP.NET MVC 表单无法正确提交/反序列化?

转载 作者:行者123 更新时间:2023-12-04 15:19:24 27 4
gpt4 key购买 nike

这看起来应该可以工作,但是提交的表单没有正确反序列化模型。使用来自 nuget 的最新 ASP.NET MVC。 .NET 4.5

一个非常标准的用户注册 View / Controller 。

查看:

@model Alertera.Portal.Web.Models.RegisterViewModel
@{
ViewBag.Title = "Register";
}

<h2>@ViewBag.Title.</h2>

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create a new account.</h4>
<hr />
@*@Html.ValidationSummary()*@
<div class="form-group">
@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
</div>
</div>


<div class="form-group">
@Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Register" />
</div>
</div>
}

Controller :

// POST: /Account/Register
[AcceptVerbs(HttpVerbs.Post)]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new User
{
UserName = model.UserName,
FirstName = model.FirstName,
LastName = model.LastName
};

user.SetEmail(model.Email);

var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_bus.Publish(new UserCreated(user));

await SignInAsync(user, isPersistent: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}

// If we got this far, something failed, redisplay form
return View(model);
}

所有的帖子都带有模型为空或伪造 token 不存在(取决于我是否启用或禁用验证)。我只是被难住了,不知道去哪里找。

** 编辑 **

如果我禁用防伪,ModelState 无效,模型中的所有字段都是空的,错误消息表明字段是必需的。

我正在将 Autofac 与 MVC 扩展一起使用,模型绑定(bind)器是这样注册的:

    builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();

Autofac 正常工作,因为 Controller 已正确实例化并注入(inject)了属性依赖项。

编辑 2:根据建议,通过从 DefaultModelBinder 继承来创建自定义 Binder ,以便我可以看到转换。看起来 bindingContenxt 的模型是空的

enter image description here

View 模型本身在这里:

public class RegisterViewModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }

[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }

[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

[Required]
public string Email { get; set; }

[Required]
public string LastName { get; set; }

[Required]
public string FirstName { get; set; }
}

最佳答案

经过一整天的故障排除、更新所有 nuget 包、确保 web.config 的所有内容都紧密且包含正确的程序集重定向、路由整洁,甚至修复 .NET 框架和其他耗时且不相关的事件,我终于想通了:

几周前,我们引入了一个 Autofac 绑定(bind),它可以捕获序列化的 HttpContext 以及其他相关数据,以供日志框架在需要时使用。 (想象一下,能够将请求信息与业务对象内的完整异常堆栈一起记录下来,而不会用 session /日志数据污染业务逻辑。)不幸的是,作为绑定(bind)创建的一部分,HttpContext 正在被 Json.net 序列化,而不是在记录事件时,而是在绑定(bind)时。

可以看出,Json.net在序列化HttpContext的时候,其实是第一次读取里面的流,导致提交的表单数据被读取,所以当Controller实例化,数据发布的时候,流已经已阅读且 Request.Form 集合为空。

只创建一个委托(delegate)来序列化 HttpContext 的简单修复似乎已经解决了这个问题

关于asp.net-mvc - 是什么导致 ASP.NET MVC 表单无法正确提交/反序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25511388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com