操作方法与视图的隐式连接.净MVC

本文关键字:连接 MVC 视图 操作方法 | 更新日期: 2023-09-27 18:07:37

我目前在棕地工作ASP。. NET MVC 3项目在VS2010.

在这个项目中,视图和控制器位于不同的项目中。这是我以前从未见过的。在每个操作方法中,没有如下所示的视图名称的显式声明。

return View("viewName",passingModel);//projects where controllers and views are in same 

我通过右键单击视图并执行add view,在VS2012中隐式地完成了此操作。所以我不担心动作方法的返回视图和视图被声明之间的联系在哪里。

与VS2012不同,在VS2010中,我无法通过右键单击视图并执行go to view来导航到与一个特定操作方法相关的视图。

我试图通过做这个小实验来理解这一点。我创建了一个Controller和一个Action Method调用xxxx,我为它隐式地创建了一个视图,如上所述,并在整个解决方案中搜索xxxx这个词,但这个词只出现在控制器和视图中。

所以,我没能找到答案。我认为visual studio本身创建了自己的映射来实现这一点。我想知道这些隐式连接是谁在操作方法和视图之间创建的,以了解我的项目中发生了什么。

编辑:

包含控制器和视图的项目都是类库。不是asp.net MVC项目。

Global.aspx文件包含:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
        protected void Application_Start()
        {
            DependenciesHelper.Register(new HttpContextWrapper(Context));
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RoutingHelper.RegisterRoutes(RouteTable.Routes);
        }
        protected void Application_End()
        {
            //Should close the index
            //If this method is not executed, the search engine will still work.
            SearchService.CloseIndex();
        }

操作方法与视图的隐式连接.净MVC

映射相当简单。例如,如果你有一个名为"MyBrilliantController"的控制器和一个名为"MyExcellentAction"的操作方法,它只返回return View();,它将映射到(在UI项目中)~/Views/MyBrilliant/MyExcellentAction.cshtml

唯一不同的地方是当你使用"区域"时-但映射实际上是相同的,它只会首先考虑区域文件夹(即~/Areas/MyArea/Views/MyBrilliant/MyExcellentAction.cshtml)

希望对你有帮助。

EDIT -您还可以在全局文件中指定名称空间。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { 
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional 
        }, // Parameter defaults
        new string[] {
            // namespaces in which to find controllers for this route
            "MySolution.MyControllersLib1.Helpers", 
            "MySolution.MyControllersLib2.Helpers",
            "MySolution.MyControllersLib3.Helpers" 
        } 
    );
}