1

我需要将不同的项目区域分成单独的程序集......我在解决视图时遇到了问题......我有一个主项目,它是一个 mvc4 项目,带有剃须刀和 IoC 的 StructureMap,我已经创建另一个 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)

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

4

2 回答 2

1

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

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

于 2015-10-21T15:48:17.507 回答
0

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

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

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

于 2013-08-06T12:00:27.807 回答