"值不能为null或为空。参数名称:contentPath"当ModelState出错时,在回发上最意外的一行

时间:2016-07-06 18:40:45

标签: asp.net .net asp.net-mvc asp.net-mvc-5

我已经连续两个小时了,我一直在讨价还价。

我收到此错误:

  

值不能为null或为空。参数名称:contentPath

在我看来的这一行:

@Html.ValidationMessageFor(model => model.IssueName, 
  "", new { @class = "text-danger" })

下面给出的堆栈跟踪似乎表明已经调用Url.Content,但我没有进行此类调用。下面是堆栈跟踪,然后是该行周围的一些代码行导致错误:

  

[ArgumentException:Value不能为null或为空。参数名称:   contentPath] System.Web.Mvc.UrlHelper.GenerateContentUrl(String   contentPath,HttpContextBase httpContext)+125
  System.Web.Mvc.UrlHelper.Content(String contentPath)+26
  ASP._Page_Views_Journal_EditIssue_cshtml.Execute()in   MyProject的\查看\杂志\ EditIssue.cshtml:45
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+197
  System.Web.Mvc.WebViewPage.ExecutePageHierarchy()+ 105   System.Web.WebPages.StartPage.RunPage()+17
  System.Web.WebPages.StartPage.ExecutePageHierarchy()+64
  System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext   pageContext,TextWriter writer,WebPageRenderingBase startPage)+78
  System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,   TextWriter writer,Object instance)+256
  System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext   viewContext,TextWriter writer)+107
  System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)   +291 System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext   controllerContext,ActionResult actionResult)+13
  System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList的1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList
1   filters,Int32 filterIndex,ResultExecutingContext preContext,   ControllerContext controllerContext,ActionResult actionResult)+420
  System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext   controllerContext,IList 1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173 System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult
1.CallEndDelegate(IAsyncResult   asyncResult)+10
  System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+29
  System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+22
  System.Web.Mvc.Async.WrappedAsyncResultBase 1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+29
  System.Web.Mvc.Async.WrappedAsyncResultBase`1.End()+49
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)   +28 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult)   结果)+9
  System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   +9644097 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155

抛出异常的行周围的代码行是:

<div class="form-group">
    @Html.LabelFor(model => model.IssueName, htmlAttributes: new { @class = "control-label col-md-2"})
    <div class="col-md-10">
        @Html.EditorFor(model => model.IssueName, new { htmlAttributes = new { @class = "form-control", id = "IssueName" } })
        @Html.ValidationMessageFor(model => model.IssueName, "", new { @class = "text-danger" })
    </div>
</div>

重要

奇怪的是,只有当我从回发后回来时才会引发异常,而且从来没有在我第一次加载视图时,甚至在ModelState没有错误时也是如此。只有当ModelState在回发后有错误时才会引发它。

这是一些服务器端代码,但我没有看到任何可能出错的原因:

[HttpPost]
public async Task<ActionResult> EditIssue(EditIssueViewModel viewModel)
{
    viewModel.AvailableTags = BusinessManager.GetAllTags();

    if (viewModel.IssuePDFFile == null || viewModel.IssuePDFFile.ContentLength == 0)
    {
        ModelState.AddModelError("", "Please select a file to upload.");
        return View(viewModel);
    }

    var fileInfo = new FileInfo(viewModel.IssuePDFFile.FileName);
    if (!StaticData.AcceptedContentTypes.Contains(viewModel.IssuePDFFile.ContentType, StringComparer.InvariantCultureIgnoreCase) ||
        !fileInfo.Extension.Equals(".pdf", StringComparison.InvariantCultureIgnoreCase))
    {
        ModelState.AddModelError("", "You can only select a PDF file.");
        return View(viewModel);
    }

    if (!ModelState.IsValid)
    {
        var errors = ModelState.Values.SelectMany(v => v.Errors)
            .Select(e => new { e.ErrorMessage, e.Exception });

        var errorList = errors.ToList();

        errorList.ForEach(e => Debug.Print(e.ErrorMessage));
        errorList.ForEach(e => ModelState.AddModelError("", e.ErrorMessage));

        return View(viewModel);
    }

    var operationResult = await BusinessManager.EditIssueAsync(viewModel);

    if (!operationResult.Succeeded)
    {
        ModelState.AddModelError("", operationResult.FailureMessage);
        return View(viewModel);
    }

    viewModel = (EditIssueViewModel)BusinessManager.GetIssueWithRelationships(viewModel.IssueId);
    viewModel.SuccessMessage = operationResult.SuccessMessage;
    return View(viewModel);
}

1 个答案:

答案 0 :(得分:35)

神圣的废话!

如果你曾经遇到过这个错误,请记住,只有一个,而且只有一个导致此错误。

  

您视图中的行网络中的某个位置已传递null   @ Url.Content方法的值。周期。

它报告异常的行可能与异常无关,可能距离罪魁祸首一两英里。

搜索,搜索,搜索疯狂。 : - )

这就是我如何找到解决方案并缓解我的痛苦。