我目前正在 VS2010 的棕色地带 ASP.NET MVC 3 项目中工作。
在此项目中,视图和控制器位于不同的项目中。这不是我以前见过的。在每个操作方法中,没有明确说明视图名称,如下所示。
return View("viewName",passingModel);//projects where controllers and views are in same
我在VS2012中通过右键单击视图并add view
隐式执行此操作。所以我并不担心动作方法的返回视图和视图之间的连接在哪里被说明。
与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();
}
答案 0 :(得分:2)
映射相当简单。例如,如果你有一个名为“MyBrilliantController”的控制器和一个名为“MyExcellentAction”的操作方法,它只返回return View();
,它将映射到(在UI项目中)~/Views/MyBrilliant/MyExcellentAction.cshtml
唯一不同的是当你使用“区域”时 - 但是映射实际上是相同的,它只会考虑区域文件夹(即~/Areas/MyArea/Views/MyBrilliant/MyExcellentAction.cshtml
)
希望有所帮助。
编辑 - 您还可以在每个路由的global.asax文件中指定名称空间,以便引擎找到控制器
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"
}
);
}