tags:

views:

813

answers:

2

I would like to reuse *.aspx files in multiple ASP.Net MVC projects. Is this possible?

EDIT: Anthony's suggestion of using version control system to share common files across multiple projects solves my question in a practical way. Luckily, since I'm using Subversion, the solution fits me. However, if I wasn't using one, how can this problem still be solved?

Is it possible to do something like this?

  1. Build a redistributable User Control using VS's website precompilation feature. (As described here.)
  2. Reference the output assemblies in the required projects.
  3. Create a modified View engine that instantiates User Controls via generic type parameter.

We then construct controller actions like this:

public ActionResult Shared()
{
    return View<SharedPageOrUserControl>();
}

Does that look possible?

+5  A: 

One idea you could try would be to:

  1. Create a project (class library) for your shared Views
  2. Add and set any *.aspx markup pages to Embedded Resources in the Properties pane
  3. Add a class "EmbeddedViewResult" that inherits from ActionResult - this would contain logic to ensure the embedded .aspx files are extracted and available on disk when called at the end of a controller action.

So in the projects that you wanted to use the shared Views, the controller actions could return something like

public ActionResult Shared()
{
    return new EmbeddedViewResult("SharedLib.SharedPage");
}

I've done something similar with WebForms pages, so it should be possible.

Jarrod Dixon
Thanks for your suggestion. I tried playing around with this solution and ran into two problems: 1.) Class Library projects seems to dislike *.aspx files (missing from the "Add New Item" dialog) and 2.) embedded *.aspx files doesn't seem to be easily editable.
David H
The "Add New Item" dialog is context sensitive; it doesn't suggest .aspx files since those have no usual relevance in a Class Library Project. But that doesn't mean a Class Library Project cannot contain them.
bzlm
Correct, the template is missing; you have to add a text file and rename it. And dealing with embedded resources in this context is a huge pain (rebuilds everywhere)! I had used this technique to package shared administrative pages, and it wasn't fun, but it paid off when used across many apps.
Jarrod Dixon
+2  A: 

I normally solve this sort of issue via source code control. Most systems (even VSS) allow you to share a file across multiple projects.

AnthonyWJones
Yep, that's how I'd probably do it, too - but Sat night is time for architect astronaut to go out and play! +1
Jarrod Dixon
Since I'm using Subversion, I suppose I can just check out *.aspx from a common repository path into each of my projects that requires them. That a neat solution. I suppose I should be doing that for my graphics, css, and js files. Thanks!
David H
-1; Sharing is something of an anti-pattern for SCM. (Checking in against one of the copies leaves your files in a inconsistent state until you remember to do a get to update the copies).
Richard
@Richard: I don't follow your reasoning the same situation arises when any file is checked in. This is approach is simple and effective for this scenario.
AnthonyWJones