Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can ASP.Net MVC Views (*.aspx) be reused in multiple ASP.net MVC projects?

Tags:

asp.net-mvc

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?

like image 507
David H Avatar asked Mar 22 '09 08:03

David H


2 Answers

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.

like image 102
Jarrod Dixon Avatar answered Nov 15 '22 20:11

Jarrod Dixon


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

like image 21
AnthonyWJones Avatar answered Nov 15 '22 19:11

AnthonyWJones