为了包含控制器,您需要更改路由注册以告诉他们在哪里寻找控制器:
routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}",
namespaces: new[] {"[Namespace of the Project that contains your controllers]"},
defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});
要包含视图,请创建自定义ViewEngine
:
public class CustomViewEngine: RazorViewEngine
{
public CustomViewEngine()
{
MasterLocationFormats = new string[]
{
"~/bin/Views/{1}/{0}.cshtml",
"~/bin/Views/{1}/{0}.vbhtml",
"~/bin/Views/Shared/{0}.cshtml",
"~/bin/Views/Shared/{0}.vbhtml"
};
ViewLocationFormats = new string[]
{
"~/bin/Areas/{2}/Views/{1}/{0}.cshtml",
"~/bin/Areas/{2}/Views/{1}/{0}.vbhtml",
"~/bin/Areas/{2}/Views/Shared/{0}.cshtml",
"~/bin/Areas/{2}/Views/Shared/{0}.vbhtml"
};
.
.
.
}
}
protected void Application_Start()
{
ViewEngines.Engines.Add(new CustomViewEngine());
有关更多信息,请查看默认实现RazorViewEngin
.
这里有一些不错的文章:
具有动态视图位置的自定义视图引擎
在 ASP.NET Web API 中使用外部程序集中的控制器
如何在 ASP.NET MVC 应用程序中调用外部程序集中的控制器
如何实现自定义 RazorViewEngine 来查找非标准位置的视图?
ASP.NET MVC 中单独程序集中的视图