在尝试访问控制器功能时获取InvalidOperationException以访问View

时间:2016-08-09 14:38:06

标签: c# asp.net asp.net-mvc asp.net-mvc-routing

MVC相当新,支持代码通常有效但产生的服务器错误过多。服务器错误采用以下形式:

Exception information: 
    Exception type: InvalidOperationException 
    Exception message: The view 'RenderProthyPDF' or its master was not     found or no view engine supports the searched locations. The following locations  were searched:
~/Views/Portal/RenderProthyPDF.aspx
~/Views/Portal/RenderProthyPDF.ascx
~/Views/Shared/RenderProthyPDF.aspx
~/Views/Shared/RenderProthyPDF.ascx
~/Views/Portal/RenderProthyPDF.cshtml
~/Views/Portal/RenderProthyPDF.vbhtml
~/Views/Shared/RenderProthyPDF.cshtml
~/Views/Shared/RenderProthyPDF.vbhtml
   at System.Web.Mvc.ViewResult.FindView(ControllerContext context)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at  System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext  controllerContext, ActionResult actionResult)

然而' RenderProthyPDF不是视图,而是控制器中的函数:

cshtml中的调用代码示例:

<a class="document-link" href="javascript:markLastVisitedCaseDocument('@currentDocument.WorkingDocumentURL'); renderProthyPDF('@currentDocument.DktYear', '@currentDocument.DktSequence', '@currentDocument.WorkingDocumentURL');">

<a class="tile" style="height: 130px;" href="javascript:renderProthyPDF('@d.DktYear', '@d.DktSequence', '@d.URL');">

引用的javascript:

    renderProthyPDF = function (rDktYear, rDktSequence, rURL) {
    jQuery.ajax({
        async: false,
        type: "POST",
        url: '@Url.Action("RenderProthyPDF", "Portal")',
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ Year : rDktYear,
                           Sequence : rDktSequence,
                  prothyDocumentURL : rURL
        }),
        success: function (data) {
            alert(data);
        },
        failure: function (errMsg) {
            alert(errMsg);
        }
    });

C#代码(在JPortal \ Controllers \ PortalController.cs中):

        public ActionResult RenderProthyPDF(string Year, string Sequence, string prothyDocumentURL, PortalCaseView viewmodel)
    {
        System.Web.HttpContext.Current.Session.Add("renderingPDF", "true");
        System.Web.HttpContext.Current.Session.Add("renderingURL", prothyDocumentURL);

        return View();
    }

我该怎么做才能防止生成这些错误?出了什么问题?

编辑:我指定的目录:

引用的超链接位于部分视图中     JPortal \视图\ Shared_.cshtml

主视图(javascript代码)位于:     JPortal \视图\门户\ Case.cshtml

ActionResult函数在     JPortal \ Conrollers \ PortalController.cs

1 个答案:

答案 0 :(得分:1)

错误消息是否足够清晰,因为它说它在找不到的下面路径中找不到名为RenderProthyPDF.cshtml的视图文件

~/Views/Portal/*
~/Views/Shared/*

在您的控制器操作方法RenderProthyPDF中,您要说的是return View();,它将查找与其无法找到的操作方法同名的视图,从而查找错误。

如果您没有视图,那么如果您使用不同的名称创建了视图,则生成一个(或),然后将该名称传递给View()方法(因为{{1的重载之一)方法接受视图名称为View()参数)

string