未找到视图或其主视图或视图引擎不支持搜索的位置

use*_*537 73 asp.net-mvc-4

错误如:未找到视图"LoginRegister"或其主控或没有视图引擎支持搜索的位置.搜索了以下位置:

〜/查看/我的帐户/ LoginRegister.aspx

〜/查看/我的帐户/ LoginRegister.ascx

〜/查看/共享/ LoginRegister.aspx

〜/查看/共享/ LoginRegister.ascx

〜/查看/我的帐户/ LoginRegister.cshtml

〜/查看/我的帐户/ LoginRegister.vbhtml

〜/查看/共享/ LoginRegister.cshtml

〜/查看/共享/ 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 }
            );
        }
    }
Run Code Online (Sandbox Code Playgroud)

Nig*_*ist 98

如果您的模型类型是String,请小心,因为View的第二个参数(字符串,字符串)是masterName,而不是model.您可能需要使用object(model)作为第二个参数调用重载:

不正确 :

protected ActionResult ShowMessageResult(string msg)
{
    return View("Message",msg);
}
Run Code Online (Sandbox Code Playgroud)

正确:

protected ActionResult ShowMessageResult(string msg)
{
    return View("Message",(object)msg);
}
Run Code Online (Sandbox Code Playgroud)

或(由bradlis7提供):

protected ActionResult ShowMessageResult(string msg)
{
    return View("Message",model:msg);
}
Run Code Online (Sandbox Code Playgroud)

  • 而不是强制转换`(object)msg`,明确指定参数:`return View("Message",model:msg);` (13认同)

gla*_*rou 29

问题:

View无法在默认位置找到.

说明:

视图应位于命名为Controller或在Shared文件夹中的同一文件夹中.

解:

将您移动ViewMyAccount文件夹或创建一个HomeController.

备择方案:

如果您不想移动View或创建新的Controller,可以查看此链接.

  • 此外,如果您遇到此问题,我发现,最好只删除您的视图.(将其内容复制到notepadd ++)转到您的控制器,然后右键单击您的actionresult并选择生成视图,然后您可以看到您的控制器在哪里查找您的视图并相应地放置您的视图. (3认同)

Cla*_*ies 13

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

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

用于路由的约定是将Controller与名称类似的类匹配,ControllerNameControllercontroller="MyAccount"表示查找类名MyAccountController.接下来是动作,它被映射到Controller类中的一个函数,该函数通常返回一个ActionResult.即action="LoginRegister"会寻找一个功能public ActionResult LoginRegister(){}控制器的类.此函数可能会返回一个View()名为Convention LoginRegister.cshtml/Views/MyAccount/文件,并将存储在该文件夹中.

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

/Controllers/MyAccountController.cs:

public class MyAccountController : Controller 
{
    public ActionResult LoginRegister()
    {
        return View();
    }
}
Run Code Online (Sandbox Code Playgroud)

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


dan*_*u11 7

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

 public ActionResult Index()
 {
     return View("~/Views/home/LoginRegister.cshtml");
 }
Run Code Online (Sandbox Code Playgroud)


Kap*_*pil 5

检查您的视图(.cshtml 文件)的构建操作它应该设置为内容。在某些情况下,我已经看到构建操作被设置为 None(错误地)并且这个特定的视图没有部署在目标机器上,即使您看到该视图存在于有效文件夹下的 Visual Studio 项目文件中

  • 就我而言,它也与“构建操作”属性相关,但我需要将其设置为“嵌入式资源” (2认同)