【问题标题】:The view 'Reporting' or its master was not found or no view engine supports the searched locations未找到“报告”视图或其主视图,或者没有视图引擎支持搜索的位置
【发布时间】:2017-03-09 08:53:03
【问题描述】:

我已经四处搜索了几天,似乎找不到适合我的问题的解决方案。我很确定它与路由有关,但我不确定。

我收到的确切错误是:

The view 'Reporting' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Reporting/Reporting.aspx
~/Views/Reporting/Reporting.ascx
~/Views/Shared/Reporting.aspx
~/Views/Shared/Reporting.ascx
~/Views/Reporting/Reporting.cshtml
~/Views/Reporting/Reporting.vbhtml
~/Views/Shared/Reporting.cshtml
~/Views/Shared/Reporting.vbhtml

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The view 'Reporting' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Reporting/Reporting.aspx
~/Views/Reporting/Reporting.ascx
~/Views/Shared/Reporting.aspx
~/Views/Shared/Reporting.ascx
~/Views/Reporting/Reporting.cshtml
~/Views/Reporting/Reporting.vbhtml
~/Views/Shared/Reporting.cshtml
~/Views/Shared/Reporting.vbhtml

Stack Trace: 


[InvalidOperationException: The view 'Reporting' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Reporting/Reporting.aspx
~/Views/Reporting/Reporting.ascx
~/Views/Shared/Reporting.aspx
~/Views/Shared/Reporting.ascx
~/Views/Reporting/Reporting.cshtml
~/Views/Reporting/Reporting.vbhtml
~/Views/Shared/Reporting.cshtml
~/Views/Shared/Reporting.vbhtml]
   System.Web.Mvc.ViewResult.FindView(ControllerContext context) +502
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +143
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +90
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +833
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +81
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +186
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +44
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137

我的 Web 应用程序中的每个页面都出现此错误,但是无论我使用 URL 还是键入 /Home/Index,我的主页都能正常工作。

我的控制器内部方法名称与我的视图名称匹配

public ActionResult Reporting()
    {
        var model = new DashboardCurrentRelease
        {
            ApplicationNames = GetApplications()
        };

        return View("Reporting", (object)model);
    }

GetApplications() 方法如下所示:

private IEnumerable<SelectListItem> GetApplications()
        {
            var applications = new DashboardViewModel();
            var applicationList = applications.DashboardCurrentReleases.Select(x => new SelectListItem
            {
                Value = x.ApplicationName.ToString(),
                Text = x.ApplicationName
            });

            return new SelectList(applicationList, "Value", "Text");
        }

这是我的模型的样子:

public partial class DashboardViewModel : DbContext
    {
        public DashboardViewModel()
            : base("name=DashboardViewModel1")
        {
        }

        public virtual DbSet<DashboardCurrentRelease> DashboardCurrentReleases { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DashboardCurrentRelease>()
                .Property(e => e.ApplicationName)
                .IsUnicode(false);

            modelBuilder.Entity<DashboardCurrentRelease>()
                .Property(e => e.Release)
                .IsUnicode(false);
        }
    }

[Table("DashboardCurrentRelease")]
    public partial class DashboardCurrentRelease
    {
        [Display(Name = "Application Name")]
        public IEnumerable<SelectListItem> ApplicationNames { get; set; }

        [Key]
        [Column(Order = 0)]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ApplicationKey { get; set; }

        [StringLength(50)]
        public string ApplicationName { get; set; }

        [StringLength(50)]
        public string Release { get; set; }

        public int? FailCnt { get; set; }

        public int? PassCnt { get; set; }

        public int? OtherCnt { get; set; }

        public int? ExecutedCnt { get; set; }

        public int? NotExecutedCnt { get; set; }
    }

我的 routingconfig 文件如下所示:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
        );
    }

对如何解决我的问题有任何想法吗?我没有正确执行我的 RouteConfig 吗?我读到它必须首先是最不具体的路线,然后是最具体的路线,但它仍然不想为我工作。

另外一个我试过但没有成功的Route Config也是这样的:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("Reporting", "{controller}/{action}",
            new
            {
                controller = "Reporting",
                action = "Reporting"
            });

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new
            {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
        );
    }

我应该提到,我已将每个视图页面的所有属性设置为内容的构建操作,之前它们是“无”,但这也没有解决问题。

编辑: 我忘了提一下,当我在本地环境中调试时它工作得非常好,当它尝试在网络服务器上运行时它开始中断。

编辑 2:我已将堆栈跟踪添加到我最初的问题以及模型和一些修饰。

编辑 3:最后一件事是,为了访问我尝试以 2 种不同方式从视图执行此操作的页面。第一种方法是:

@Html.ActionLink("Report Details", "Reporting", "Reporting")

第二个在做:

<a href="../Reporting/Reporting">
                                        <span>Report Details</span>
                                    </a>

【问题讨论】:

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


    【解决方案1】:

    在MVC中,用于解析传入和传出URL组合的路由引擎,如果遵循路由引擎使用的约定(规则),则不必更改配置。我真的只对 MVC 约定之外的区域或任何内容使用自定义路由。而且您的约定似乎遵循 MVC 的指导方针,文件夹和控制器相互匹配。所以我认为您不需要自定义路线。

    ASP.net MVC 的路由引擎不提供网页 (.cshtml)。它提供了一种由代码中的类处理 URL 的方法,该类可以将 text/html 呈现到输出流,或者使用约定以一致的方式解析和提供 .cshtml 文件。您的视图引擎是从以控制器命名的目录中提供网页 (.cshtml) 文件的引擎。这就是说路由引擎控制 URL。你试过**return View("NameOfView", Model);**

    您需要注意其他事情,例如确保命名空间正确,以及您是否从模型中获取字符串?你们中的它必须像return View("Message",(object)msg);而不是return View("Message",msg);这样正确格式化,因为我看不到您的模型或您的视图,并且没有发布完整的堆栈跟踪,很难说为什么您的视图没有显示。当然,如果您显示一个正在以更改名称调用的 URL,那也很好。但我能想到的主要是模型和/或命名空间,此外,如果您从视图中调用 Layout = "~/Views/Shared/_Layout.cshtml"; 或者您是否依赖于 _ViewStart 文件。还要检查您是否从菜单或链接中调用了正确的操作。 @Html.Action 方法实际上调用了一个控制器并呈现视图,您是否尝试使用智能感知来查看 ActionLink 的重载(@Html.ActionLink("Some text","Action","Controller", new {id=1}, null)),只是为了确保您从链接调用正确的操作。希望这种方式可以为您指明解决方案的正确方向。

    【讨论】:

    • 感谢您的建议,我会尝试一下,这无疑帮助我朝着正确的方向前进。一旦我尝试返回“视图名称”,模型,我会添加评论,看看是否有帮助。
    • 我已经尝试在代码中同时返回视图和对象,但它似乎仍然不起作用。我在原始问题中添加了堆栈跟踪(没有意识到我没有复制整个错误)。我还将模型添加到问题中。
    • 我犯了一个愚蠢的错误,我才意识到它是什么,但是你的回答让我走上了正确的轨道,找出错误是什么,所以我把你的错误标记为正确答案..
    • 请问你发现了什么?
    • 我没有为我的页面正确配置某些属性。一旦我修复了属性,它就开始工作了,尽管我确实根据您的回答对代码进行了一些调整以稍微清理它。
    猜你喜欢
    • 2013-08-18
    • 2013-06-23
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多