ASP.NET MVC Html.Action渲染帖子而不是GET

时间:2022-11-30 22:56:56

I have blog and sometimes users catch error which i can't reproduce.

我有博客,有时用户会发现我无法重现的错误。

Simple PostItem.cshtml:

简单的PostItem.cshtml:

"Create", "Comment" - render CreateComment form

“创建”,“评论” - 渲染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

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>
}

Then i have CommentController.cs

然后我有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));
        }

As you see - all actions go out with redirects! But some users catch error, that redirect it on the main page without save comment and i have this trace in my logs. How it possible ?

正如你所看到的 - 所有行动都是重定向的!但是有些用户会捕获错误,将其重定向到主页面而不保存注释,并且我在日志中有此跟踪。怎么可能?

HTTP_X_ORIGINAL_URL /comment/create

HTTP_X_ORIGINAL_URL / comment / create

REQUEST_METHOD GET

REQUEST_METHOD GET

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)

1 个解决方案

#1


0  

Make postID nullable as shown :

使postID可以为空,如下所示:

 public PartialViewResult Create(int? postID, string returnUrl)
 public PartialViewResult CommentsForPost(int? postID)

OR if you don't want to make postID nullable then make sure that there is some integer value in Model.ID when you are passing postID in @Html.Action()

或者,如果您不想使postID成为可空,那么当您在@Html.Action()中传递postID时,请确保Model.ID中有一些整数值

#1


0  

Make postID nullable as shown :

使postID可以为空,如下所示:

 public PartialViewResult Create(int? postID, string returnUrl)
 public PartialViewResult CommentsForPost(int? postID)

OR if you don't want to make postID nullable then make sure that there is some integer value in Model.ID when you are passing postID in @Html.Action()

或者,如果您不想使postID成为可空,那么当您在@Html.Action()中传递postID时,请确保Model.ID中有一些整数值