Reputation: 3907
I'm in the need of separating different project areas into separate assembies...I've got a problem resolving the view.. I've got the main project that's an mvc4 project with razor and StructureMap for IoC,I've created another MVC4 project where I've removed the config files and the App_Start foler...created a test class called AdminController under Controllers folder and under Views\Admin a file called 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
My 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();
}));
}
}
When I go to /Admin I got
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
I don't want to put the Views in the main project...for scalability... I wish to keep them inside the assembly or even better in another assembly (maybe using RazorGenerator)
Anyone can help me on this fist step of getting the view loaded? Thanks
Upvotes: 1
Views: 2357
Reputation: 10313
Use can use RazorGenerator in order to have (pre-compiled) views in different assemblies. Have a look at Usage in a View Library to see how.
Basically, the trick is to then consume the RazorGenerator.Mvc NuGet which will register a view engine and do the VirtualPathProvider stuff for you.
Upvotes: 1
Reputation: 23084
Your views are not going to end up in the assembly unless you set each view file's 'Build Action' to "Embedded Resource" in Visual Studio (right-click the file, select properties). But then the views are still not going to be found by the runtime, since the default behavior is to look for views on disk, not inside assemblies. But this behavior can be changed.
So if you are really set on embedding the views inside your assembly, then you will have to implement a VirtualPathProvider and register it when the main application starts. All providers are given the chance to provide a 'virtual file', so your provider will get a chance to stream the view file out of the assembly (if and when the view is requested by the runtime).
An alternative is to add a post build event to your secondary project, that simply copies everything in the Views folder to the main project. In that case, I would advice you to use MVC areas, to prevent any conflicts when files are copied. This means you don't have to remember to embed view files, and you don't have to write the virtual path provider.
Upvotes: 0