ASP.NET MVC4不会在不同的程序集中加载视图

时间:2013-08-06 07:34:40

标签: asp.net view .net-assembly

我需要将不同的项目区域分成单独的组合......我在解决视图方面遇到了问题。 我有一个主要项目,它是一个带有razor和StructureMap for IoC的mvc4项目,我创建了另一个MVC4项目,我删除了配置文件和App_Start foler ...在Controllers文件夹下创建了一个名为AdminController的测试类,在Views \ Admin下一个名为Index.cshtml的文件......

AdminController.cs:

public class AdminController : Controller
{
    //
    // GET: /Admin/

    public ActionResult Index()
    {
        return View();
    }
[...omiss...]
}

Index.cshtml:

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>
That's the admin view

我的Global.asax:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RouteConfig.RegisterRoutes(RouteTable.Routes);

        //WebApiConfig.Register(GlobalConfiguration.Configuration);
        //FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);


        WireUpDependencyInjection();
        ControllerBuilder.Current.SetControllerFactory(new ControllerFactory());
    }

    private void WireUpDependencyInjection()
    {

        ObjectFactory.Initialize(registry => registry.Scan(x =>
        {
            x.AssembliesFromApplicationBaseDirectory();
            x.WithDefaultConventions();
        }));
    }
}

当我去/ Admin我有

Server Error in '/' Application.`enter code here`
--------------------------------------------------------------------------------


The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/admin/Index.cshtml
~/Views/admin/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/admin/Index.cshtml
~/Views/admin/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 



[InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/admin/Index.aspx
~/Views/admin/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/admin/Index.cshtml
~/Views/admin/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml]
   System.Web.Mvc.ViewResult.FindView(ControllerContext context) +506
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +230
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +74
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +388
   System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +72
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +303
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +155
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +184
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +151
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9628700
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我不想将视图放在主项目中...以获得可伸缩性...我希望将它们保留在程序集中,或者更好地保存在另一个程序集中(可能使用RazorGenerator)

任何人都可以帮助我完成加载视图的第一步吗? 感谢

2 个答案:

答案 0 :(得分:1)

除非在Visual Studio中将每个视图文件的“构建操作”设置为“嵌入式资源”,否则您的视图不会在程序集中结束(右键单击该文件,选择属性)。但是运行时仍然无法找到视图,因为默认行为是在磁盘上查找视图,而不是在程序集内查找视图。但是这种行为可以改变。

因此,如果你真的开始在程序集中嵌入视图,那么你必须实现VirtualPathProvider并在主应用程序启动时注册它。所有提供者都有机会提供“虚拟文件”,因此您的提供者将有机会将视图文件流出程序集(如果运行时请求视图)。

另一种方法是在二级项目中添加一个post build事件,它只是将Views文件夹中的所有内容复制到主项目中。在这种情况下,我建议您使用MVC areas,以防止复制文件时发生任何冲突。这意味着您不必记住嵌入视图文件,也不必编写虚拟路径提供程序。

答案 1 :(得分:1)

使用可以使用RazorGenerator以便在不同的程序集中拥有(预编译的)视图。请查看Usage in a View Library以了解具体方法。

基本上,诀窍就是消耗RazorGenerator.Mvc NuGet,它会注册一个视图引擎,并为你做一些VirtualPathProvider。