【问题标题】:ASP.NET MVC4 doesn't load view in a different assemblyASP.NET MVC4 不会在不同的程序集中加载视图
【发布时间】:2013-08-06 07:34:40
【问题描述】:

我需要将不同的项目区域分成不同的程序集......我在解决视图时遇到了问题...... 我有一个主项目,它是一个 mvc4 项目,带有 razor 和 StructureMap for IoC,我创建了另一个 MVC4 项目,在那里我删除了配置文件和 App_Start 文件夹...在 Controllers 文件夹下创建了一个名为 AdminController 的测试类,并且在 Views\Admin 下有一个名为 Index.cshtml 的文件...

AdminController.cs:

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

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

索引.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)

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

【问题讨论】:

    标签: asp.net view .net-assembly


    【解决方案1】:

    使用可以使用RazorGenerator 以便在不同的程序集中拥有(预编译的)视图。看看Usage in a View Library 看看如何。

    基本上,诀窍是使用RazorGenerator.Mvc NuGet,它将注册一个视图引擎并为您执行 VirtualPathProvider 的工作。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        • 2011-01-09
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多