5

I would like to separate the Views for the Static Pages in my DXA Solution to be in a separate project, and use the main project for the DXA Framework configuration and then reference the Views project.

I've tried adding a project reference, but that didn't work, and my views were not found.

I'm using this solution as a guide to get started, but for now I'm creating a new, vanilla DXA Solution and trying to set it up as clean as possible.

7

I haven't tried this with DXA specifically, but we have once implemented a .NET mvc site where views were compiled, and added to the main project as a dll.

The tool we used to do this is called RazorGenerator - https://github.com/RazorGenerator/RazorGenerator.

If I recall well, we had our main mvc website project was referencing our view project, which contained all of the views. As a result, a dll containing all views was copied to our build folder whenever we did a new build.

One of the advantages you get with this is that you get compile time checking of your view code as well.

| improve this answer | |
5

MVC.NET doesn't support this. Pretty well explained here: https://stackoverflow.com/questions/24341336/is-it-possible-to-access-mvc-views-located-in-another-project

For the lazy ones:

MVC does not compile views into DLL's, but instead references them as files from the root of your site directory. The location, by convention is ~/Views and a search path is followed. This is more or less hard coded into the default view engines.

Because Views are files, when you break them into a separate project, they won't exist in your primary web application project. Thus, the view engine can't find them. When you compile the app, any projects referenced will only copy the DLL's (and potentially a few other things, like pdb's, etc.)

The best you can do is to separate your project into several Areas.

EDIT as others suggested you could have a post build action to copy your views to the main project.

| improve this answer | |
  • 1
    I had also already read that about MVC, but seems that the word around the DXA campfire was to use separate projects for Modules, and was wondering how the campers managed to do that.... – robrtc Jun 30 '17 at 9:24
4

DXA View files are typically cshtml files. For the DXA Web Application to render the views, these files needs to be physically available in the Areas/Views folder.

If you like to maintain the View cshtml files in a separate Project, then you also need to write a post build action in your new project to copy all the view.cshtml files to the DXAWebApp project's Areas/Views folder.

| improve this answer | |
4

During DXA development we reference our module projects in the main DXA solution, and then have a post build step in all module projects, to copy the Views over to the Area folder of the DXA project, so we have everything in the right place at the right time.

update

Just noticed this is basically the same answer @Rajesh gave ;o)

| improve this answer | |
  • 3
    In your defence: what we do with DXA Modules goes further than only Views: Modules can also contain View Models and Controllers (which are compiled into a DLL). The post-build step includes copying that DLL. I think it makes sense to use the same approach (i.e. make a genuine Module) instead of having a separate project with only Views. – Rick Pannekoek Jun 30 '17 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.