【问题标题】:The view 'Home' or its master was not found or no view engine supports the searched locations. The following locations were searched: 'sporadic error'未找到视图“主页”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下位置:“零星错误”
【发布时间】:2012-10-04 22:01:59
【问题描述】:

在尝试返回视图时,我在使用 MVC 4 应用程序时遇到零星错误。

在这种特殊情况下,我将返回一个视图return View("Home", model);,这就是我得到消息的地方。当您不断测试和调试时,它似乎也偶尔发生,我认为 View Engine 变得疯狂。比如在这个之前,我在执行一个简单的视图时说它一直在那里找不到它。在清除缓存、重新启动等组合并执行相同的确切逻辑后,它工作了。

所以,我不知道如何使用 View Engine 解决这个问题。在我回到视图之前,我可以向你保证,我的模型中有记录。我无法通过我的电脑在此表单上附加屏幕打印 --- 没有选项。

所以,可怕的问题是:我该如何解决这个问题,它不会像这样偶尔发生???这是一个严重的问题,希望能得到解决....

我查看了 Scott Hanselmans 用于预编译视图等的 nuget 包,但似乎过于复杂和额外的工作。我想知道是否还有其他我可以做的。

任何帮助将不胜感激......

这是我的 Globla.asax 文件:

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

    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

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

如果有人可以回答,我将不胜感激,因为这会使我们正在开发的 MVC 应用程序停止!

我尝试将.DataTokens.Add("area", "YOURAREANAME"); 添加到MapRoute 的末尾,但我不知道用什么替换字符串。

另外,我不知道为什么需要这样做(如果它会修复它)并且需要某人的解释......

为想要检查控制器代码的另一个人添加了代码。

[HttpPost]
        public ActionResult Refresh(ViewModelTemplate_Guarantors model)
        {
            try
            {
                model.Error = string.Empty;

                bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));

                if (!dbHasRows)
                {
                    ViewBag.ShowTemps = false;
                    model.Error = "Details not available for this LoanId.";
                    return View("Home", model);
                }
                else
                {
                    int TemplateCnt = 0;
                    int GuarantorCnt = 0;
                    ViewBag.ShowTemps = true;

                    ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "1", model.SelectedDeptText, out TemplateCnt, out GuarantorCnt);

                    if (TemplateCnt > 0)
                        model.Templates = tg.Templates;
                    else 
                       model.ErrorT = "Templates not available for this LoanType.";

                    if (GuarantorCnt > 0)
                        model.Guarantors = tg.Guarantors;
                    else
                        model.ErrorG = "Guarantors not available for this LoanId.";

                    return View("Home", model);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

当我的目录结构清晰时,我不明白为什么路由引擎会尝试转到以下内容: Views/Home/Index.cshtml

给出的错误如下,甚至没有寻找“索引”页面..

~/Views/Home/Home.aspx
~/Views/Home/Home.ascx
~/Views/Shared/Home.aspx
~/Views/Shared/Home.ascx
~/Views/Home/Home.cshtml
~/Views/Home/Home.vbhtml
~/Views/Shared/Home.cshtml
~/Views/Shared/Home.vbhtml

【问题讨论】:

  • 你能在你调用return View("Home", model);的地方包含控制器代码吗?
  • 我的代码包含在我的编辑的底部。如果我应该在其他地方添加编辑,请告诉我。如果您没有尽快看到编辑(在我的帖子底部),请告诉我。因为我刚刚发布了它...非常感谢您查看它...
  • 必须有一种方法可以在项目中以某种方式解决此问题,并告知其默认视图是什么。
  • 您可以清楚地看到它导致错误的原因,但我不知道为什么......
  • msg 正在寻找“Home”作为操作方法,而在 Global.asax 文件中它清楚地指定了以下内容:我该如何更正:routes.MapRoute(name:"Default", url : "{controller}/{action}/{id}", 默认值: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

标签: asp.net-mvc view asp.net-mvc-routing viewengine


【解决方案1】:

替换这个:

return View("Home", model);

用这个:

return View("Index", model);

完整代码:

public ActionResult Refresh(ViewModelTemplate_Guarantors model)
{
    try
    {
        model.Error = string.Empty;

        bool dbHasRows = db.ChkLoanFields(Convert.ToInt32(model.LoanId));

        if (!dbHasRows)
        {
            ViewBag.ShowTemps = false;
            model.Error = "Details not available for this LoanId.";
            return View("Home", model);
        }
        else
        {
            int TemplateCnt = 0;
            int GuarantorCnt = 0;
            ViewBag.ShowTemps = true;

            ViewModelTemplate_Guarantors tg = db.SelectViewModelTemplate_Guarantors(Convert.ToInt32(model.LoanId), "1", model.SelectedDeptText, out TemplateCnt, out GuarantorCnt);

            if (TemplateCnt > 0)
                model.Templates = tg.Templates;
            else
                model.ErrorT = "Templates not available for this LoanType.";

            if (GuarantorCnt > 0)
                model.Guarantors = tg.Guarantors;
            else
                model.ErrorG = "Guarantors not available for this LoanId.";

            return View("Index", model);
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

【讨论】:

    猜你喜欢
    • 2013-06-23
    • 2013-08-18
    • 2017-03-09
    • 2015-07-09
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多