该应用程序可以正确构建并运行。当我尝试在调试中提交表单时,应用程序不仅未达到验证程序代码中的断点,而且还恢复为已删除文件中的代码,该文件由于空对象而引发异常。我已经清除了所有缓存,清理了解决方案,重建了解决方案,重新启动了VS2017,重新启动了计算机,使用新用户创建了解决方案,检查了月相,似乎没有什么可以解决此问题。
我有一个简单的表单验证器,如下所示:
//模型中的注释:
[CustomValidator(ErrorMessage = "PIN must not be the same as your phone number.")]
//验证程序文件
使用系统; 使用System.ComponentModel.DataAnnotations;
命名空间MyWeb.Models { 内部类CustomValidator:ValidationAttribute { 受保护的重写ValidationResult IsValid(对象值,ValidationContext validateContext) {
var model = (Model)validationContext.ObjectInstance;
string phone = string.Empty; // break point set here
string PIN = string.Empty;
string last4 = string.Empty;
try
{
phone = model.PrimaryPhone;
PIN = model.PIN;
return ValidationResult.Success; // defaults to always true for testing
}
catch (Exception)
{
throw;
}
}
}
}
我希望正确的验证程序代码可以检查字段数据并返回true,无论输入的值如何。
//Server Error in '/' Application.
//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.]
// TelemojiWeb.Models.CustomPINValidator.IsValid(Object value, ValidationContext validationContext) in C:\src\TelemojiAll\TelemojiWeb\Models\CustomPINValidator.cs:27
// System.ComponentModel.DataAnnotations.ValidationAttribute.GetValidationResult(Object value, ValidationContext validationContext) +29
// System.Web.Mvc.DataAnnotationsModelValidator.Validate(Object container) +138
// System.Web.Mvc.<Validate>d__1.MoveNext() +237
// System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +538
// System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +68
// System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197
// System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
// System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +330
// System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
// System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
// System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
// System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
// System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
// System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
// System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
// System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
// System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
// System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
// System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
// System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
// System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
// System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
// System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
// System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
// System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
// System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
// System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
// System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
// System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
// System.Web.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0() +26
// System.Web.StepInvoker.Invoke(Action executionStep) +101
// System.Web.<>c__DisplayClass4_0.<Invoke>b__0() +22
// Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule.OnExecuteRequestStep(HttpContextBase context, Action step) +64
// System.Web.<>c__DisplayClass284_0.<OnExecuteRequestStep>b__0(Action nextStepAction) +54
// System.Web.StepInvoker.Invoke(Action executionStep) +85
// System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +9980101
// System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
//
//Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0