微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net – “值不能为null或为空.当ModelState出错时,返回时最意外的行上的参数名称:contentPath“

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

我收到此错误

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,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返回时才会引发异常,从来没有在我第一次加载视图时,甚至在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.

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

搜索,搜索,搜索疯狂.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐