asp.net - 当 ModelState 有错误时,"Value cannot be null or empty. Parameter name: contentPath"在回发时最意外的行上

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

我已经连续两个小时与这个怪物讨价还价了。

我收到此错误:

Value cannot be null or empty. Parameter name: contentPath



在我看来这一行:
@Html.ValidationMessageFor(model => model.IssueName, 
  "", new { @class = "text-danger" })

下面给出的堆栈跟踪似乎表明调用 Url.Content已经打过,但我没有打过这样的电话。下面是堆栈跟踪,然后是导致错误的行周围的更多代码行:

[ArgumentException: Value cannot be null or empty. Parameter name: 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\Views\Journal\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(IList1 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, IList1 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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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.WrappedAsyncResultBase1.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 result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9644097 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& 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>

重要信息

奇怪的是,只有当我从 post-back 返回时才会引发异常,而不会在我第一次加载 View 时引发异常,甚至在 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);
}

最佳答案

碉堡了!

如果您曾经,曾经,曾经遇到过此错误,请记住,导致此错误的原因只有一个,并且只有一个。

Somewhere in the network of lines on your view, you have passed a null value to the @Url.Content method. Period.



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

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

这就是我如何找到解决方案并从我的痛苦中解脱出来的方法。

关于asp.net - 当 ModelState 有错误时,"Value cannot be null or empty. Parameter name: contentPath"在回发时最意外的行上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38231453/

相关文章:

.net - VB.NET:如果我总是使用 Thread.MemoryBarrier() 完成写入,我是否需要在每次读取之前调用 Thread.MemoryBarrier()?

asp.net-mvc - 在 ASP.NET MVC 3 中路由静态文件,如 robots.txt

c# - 如何在 Startup.cs 文件中获取 ASP.NET MVC 应用程序的基本 url?

asp.net-mvc - Entity Framework 6 在尝试使用 Effort 创建上下文时抛出迁移异常

c# - 无法在 Visual Studio 2012 Professional 中导入数据库

javascript - 聚焦在asp.net中的文本框后,如何在css中激活div的功能?

asp.net - ASP核心: How to set httpContext.用户?

asp.net - 我怎样才能把它变成一个div

c# - 实现资源管理器ContextMenu并将多个文件传递到一个程序实例

c# - 在 C# 解决方案中创建 PFX 文件