第二次尝试注册后出错

时间:2011-08-28 07:25:36

标签: asp.net-mvc-3 asp.net-membership

我在MVC 3.0中使用自定义asp.net成员资格

注册运行良好没有问题,但在测试期间我发现了一个问题 如果我输入不正确的值,例如密码不符合asp.net会员资格设置标准,那么我得到服务器验证错误,说我需要修复:

Your account wasn't created. Please correct the errors and try again. 
The password provided is invalid. Please enter a valid password value.

那么如果再输入另一个密码并单击“提交”,我将收到此错误:

Object reference not set to an instance of an object. 
 Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 
 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 


Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   System.Web.Security.MachineKey.Decode(String encodedData, MachineKeyProtection protectionOption) +269
   Microsoft.Web.Mvc.MachineKeyWrapper.Decode(String encodedData, MachineKeyProtection protectionOption) +10
   Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode, IMachineKey machineKey) +80

[SerializationException: Deserialization failed. Verify that the data is being deserialized using the same SerializationMode with which it was serialized. Otherwise see the inner exception.]
   Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode, IMachineKey machineKey) +232
   Microsoft.Web.Mvc.MvcSerializer.Deserialize(String serializedValue, SerializationMode mode) +25
   Microsoft.Web.Mvc.DeserializingModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +128

.....

任何想法可能是什么?

我的控制器看起来像那样:

public virtual ActionResult Register(RegisterModel model, [Deserialize]List<MyModelState> myModelState)
{
  //to stuff
  //if error save ModelStt in temp data to render it back to hidden form value
  TempData["MyModelState"] = myModelState;
}

在视图上我这样做:

@using (Html.BeginForm())
{
    List<MyModelState> hidden = TempData["MyModelState"] as List<MyModelState>;

    if (hidden != null) //never null 
    {        
        @Html.Serialize("MyModelState", hidden)
    } 
}

我认为问题[Deserialize]List<MyModelState>但是我无法理解为什么它可能会出现因为隐藏值表示该对象永远不会为空并始终以表格形式出现:

<input name="MyModelState" type="hidden" value="2CF80623577311BA401E41727261794F66537562736372697074696F6E44657461...../>

我在错误发生之前检查表单中的MyModelState的值,当它发生时我单击浏览器按钮返回以确保隐藏值在表单中并且等于错误之前的值和它没事,在那里看不到坏事。

1 个答案:

答案 0 :(得分:0)

我也有同样的例外。

我将web.config,sessionState模式从“InProc”更改为“StateServer”。

<configuration>
  <system.web>
    <sessionState mode="StateServer" .../>
    ...

(当然,会话状态模式不一定是“SateServer”,你可以选择“SQLServer”或“自定义”。)

然后,它看起来像是固定的。

请尝试。