我有博客,有时用户会收到我无法重现的错误。
简单的PostItem.cshtml:
“创建”,“评论” - 渲染CreateComment表单
@Html.Action("Create", "Comment", new { @returnUrl = Request.Url, @postID = Model.ID })
<div name="#comments">
@Html.Action("CommentsForPost", "Comment", new { postID = Model.ID })
</div>
CreateComment.cshtml
<div class="create-comment-container">
@using (Html.BeginForm("Create", "Comment", FormMethod.Post, new { role = "form" }))
{
@Html.Hidden("returnUrl", Request.QueryString["returnUrl"])
@Html.HiddenFor(m => m.PostID)
.........
<button type="submit" class="btn btn-default pull-right">@L("Post")</button>
}
然后我有CommentController.cs
public PartialViewResult Create(int postID, string returnUrl)
{
return this.PartialView("Partials/CreateComment", new Comment { PostID = postID });
}
[HttpPost]
[ValidateInput(false)]
public async Task<ActionResult> Create(Comment comment, string returnUrl)
{
var validator = ValidationFactory.CreateValidator<Comment>();
var results = validator.Validate(comment);
if (results.IsValid)
{
var result = await CommentManager.CreateComment(comment);
if (result != null && !String.IsNullOrWhiteSpace(returnUrl))
return Redirect(returnUrl + "#comments");
}
return Redirect("/");
}
public PartialViewResult CommentsForPost(int postID)
{
return PartialView("CommentsForPost", CommentManager.GetComments(postID, PaginationHelper.GetCurrentPageForName(ControllerContext.RequestContext, "commentPage"), BlogSettings.CommentPageSize));
}
如您所见 - 所有操作都会重定向! 但是有些用户会捕获错误,将其重定向到主页面而不保存注释,并且我在日志中有此跟踪。 怎么可能?
HTTP_X_ORIGINAL_URL / comment / create
REQUEST_METHOD 获取
System.ArgumentException
The parameters dictionary contains a null entry for parameter 'postID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult Create(Int32, System.String)' in 'geenBlog.Web.Controllers.CommentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
System.ArgumentException: The parameters dictionary contains a null entry for parameter 'postID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.PartialViewResult Create(Int32, System.String)' in 'geenBlog.Web.Controllers.CommentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
at System.Web.Mvc.ActionDescriptor.ExtractParameterFromDictionary(ParameterInfo parameterInfo, IDictionary`2 parameters, MethodInfo methodInfo)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
答案 0 :(得分:0)
如图所示postID nullable
:
public PartialViewResult Create(int? postID, string returnUrl)
public PartialViewResult CommentsForPost(int? postID)
或者,如果您不想postID nullable
,那么当您在Model.ID
@Html.Action()
中有一些整数值>