Asp.net mvc 4 未找到视图或其主视图,或者没有视图引擎支持搜索的位置

Asp.net mvc 4 未找到视图或其主视图,或者没有视图引擎支持搜索的位置,asp.net-mvc-4,Asp.net Mvc 4,错误如下:未找到视图“LoginRegister”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下地点: ~/Views/MyAccount/LoginRegister.aspx ~/Views/MyAccount/LoginRegister.ascx ~/Views/Shared/LoginRegister.aspx ~/Views/Shared/LoginRegister.ascx ~/Views/MyAccount/LoginRegister.cshtml ~/Views/MyAcc

错误如下:未找到视图“LoginRegister”或其主视图,或者没有视图引擎支持搜索的位置。搜索了以下地点:

~/Views/MyAccount/LoginRegister.aspx

~/Views/MyAccount/LoginRegister.ascx

~/Views/Shared/LoginRegister.aspx

~/Views/Shared/LoginRegister.ascx

~/Views/MyAccount/LoginRegister.cshtml

~/Views/MyAccount/LoginRegister.vbhtml

~/Views/Shared/LoginRegister.cshtml

~/Views/Shared/LoginRegister.vbhtml

实际上,我的页面视图页面是
~/Views/home/LoginRegister.cshtml
,所以我做什么

我的
RouteConfig

 public class RouteConfig
    {

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

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

问题:

在默认位置找不到您的
视图

说明:

视图应位于名为
控制器
的同一文件夹中,或位于
共享
文件夹中

解决方案:

将您的
视图
移动到
MyAccount
文件夹或创建
HomeController

备选方案:


如果你不想移动你的
视图
或创建一个新的
控制器
,你可以检查一下。

在Microsoft ASP.net MVC中,用于解析传入和传出URL组合的路由引擎是按照约定优先于配置的思想设计的。这意味着,如果您遵循路由引擎使用的约定(规则),则不必更改配置

ASP.net MVC的路由引擎不提供网页(.cshtml)。它提供了一种由代码中的类处理URL的方法,该类可以向输出流呈现文本/html,或者使用约定以一致的方式解析和提供.cshtml文件

用于路由的约定是将控制器与名称类似于
ControllerNameController
的类相匹配,即
Controller=“MyAccount”
表示查找名为
MyAccountController
的类。接下来是action,它被映射到Controller类中的一个函数,该函数通常返回一个
ActionResult
。i、 e.
action=“LoginRegister”
将在控制器的类中查找函数
public ActionResult LoginRegister(){}
。此函数可能返回一个
视图()
,按照惯例,该视图名为
LoginRegister.cshtml
,存储在
/Views/MyAccount/
文件夹中

总而言之,您将拥有以下代码:

/控制器/MyAccountController.cs:

public class MyAccountController : Controller 
{
    public ActionResult LoginRegister()
    {
        return View();
    }
}

/Views/MyAccount/LoginRegister.cshtml:您的视图文件。

如果您的模型类型是String,请小心,因为视图的第二个参数(String,String)是masterName,而不是model。您可能需要使用对象(模型)作为第二个参数调用重载:

不正确:

protected ActionResult ShowMessageResult(string msg)
{
    return View("Message",msg);
}
正确:

protected ActionResult ShowMessageResult(string msg)
{
    return View("Message",(object)msg);
}
或(由bradlis7提供):

检查您创建的视图(.ASPX文件)是否与控制器中提到的名称相同。例如:

 public ActionResult GetView()
 {
    return View("MyView");
 }

在这种情况下,aspx文件的名称应为MyView.aspx,而不是GetView.aspx,这可能是权限问题


我最近也有同样的问题。作为测试,我创建了一个简单的hello.html页面。当我尝试加载它时,我收到一条关于权限的错误消息。一旦我修复了根web文件夹中的权限问题,html页面和MVC呈现问题都得到了解决

在您返回视图时的LoginRegister操作中,执行以下操作,我知道这可以在mvc 5中完成,我不确定是否也可以在mvc 4中完成

 public ActionResult Index()
 {
     return View("~/Views/home/LoginRegister.cshtml");
 }

如果您已经检查了上述答案中的所有内容(这些都是常见错误),并且确定您的视图位于异常中的位置,那么您可能需要重新启动Visual Studio


:(

我得到这个错误是因为我重命名了我的视图(和后期操作)

最后,我发现我忘记将GET和POST操作重命名为新名称


解决方案重命名GET和POST操作以匹配视图名称。

如果问题在生产过程中间歇性发生,可能是由于操作方法被中断。例如,在涉及大文件上载的POST操作期间,用户在上载完成之前关闭浏览器窗口在这种情况下,action方法可能会抛出由null模型或视图对象导致的null引用异常。解决方案是将方法体包装在try/catch中并返回null。如下所示:

[HttpPost]
public ActionResult Post(...)
{
    try
    {
        ...
    }
    catch (NullReferenceException ex)  // could happen if POST is interrupted
    {
        // perhaps log a warning here
        return null;
    }

    return View(model);
}

检查视图(.cshtml文件)的生成操作。它应设置为内容。在某些情况下,我看到生成操作被设置为无(错误),并且此特定视图未部署在目标计算机上,即使您在visual studio项目文件的有效文件夹下看到该视图,我也遇到了同样的问题。 我复制了一个视图“Movie”,并相应地将其重命名为“Customer”。 我对模型和控制器也做了同样的操作

解决方案是这样的…我将Customer视图重命名为Customer1,然后 刚刚创建了一个新的视图并称之为Customer…然后我就复制了它 将Customer1代码转换为Customer

这起作用了

我很想知道问题的真正原因

[HttpGet]
[ControleDeAcessoAuthorize("Report/ExportToPDF")]
public ActionResult ExportToPDF(int id, string month, string output)
{
    try
    {
        // Validate
        if (output != "PDF")
        {
            throw new Exception("Invalid output.");
        }
        else
        {
            ...// code to generate report in PDF format
        }
    }
    catch (Exception ex)
    {
        return RedirectToAction("Error");
    }
}

[ControleDeAcessoAuthorize("Report/Error")]
public ActionResult Error()
{
    return View();
}
更新
只是为了微笑……我回去再次复制了所有重命名场景……没有出现任何错误。

在我的情况下,我需要使用RedirectToAction来解决问题

[HttpGet]
[ControleDeAcessoAuthorize("Report/ExportToPDF")]
public ActionResult ExportToPDF(int id, string month, string output)
{
    try
    {
        // Validate
        if (output != "PDF")
        {
            throw new Exception("Invalid output.");
        }
        else
        {
            ...// code to generate report in PDF format
        }
    }
    catch (Exception ex)
    {
        return RedirectToAction("Error");
    }
}

[ControleDeAcessoAuthorize("Report/Error")]
public ActionResult Error()
{
    return View();
}

不久前我遇到了这个问题,它让我抓狂,因为它原来很简单。因此,在我的视图中,我使用了一个网格控件,该控件通过http请求获取网格数据。一旦中间层完成请求并返回数据集,我就收到了相同的错误。结果证明,我的返回语句是“返回视图(数据集)”而不是“retu”