views:

127

answers:

3

I tried the following:

I have a shared library (.dll) that contains these files:

  • Views
    • Search
      • PowerSearch.aspx
      • PowerSearch.aspx.cs
      • PowerSearch.aspx.designer.cs

The PowerSearch.aspx file contains my html code. The PowerSearch.aspx.cs file contains this:

using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;

namespace CommonProject.Web.Shared.Views.Search
{
 public partial class PowerSearch : ViewPage<SearchViewData>{}
}

And the designer I catually don't even care about cause it's not used anyways.

Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".

Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp @Page rule.

The matching cs file contains:

using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;

namespace CommonProject.Web.DRE.Views.Search
{
 public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}

There are no compile errors and no run time exceptions either. There is only a huge blank page...

Anybody got an idea?

A: 

Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.

<pages>
      <namespaces>
        <add namespace="CommonProject.Web.Shared"/>
David Liddle
The dll is in the bin folder. I didn't add the namespace though. But with our without the namespace rule doesn't matter.The annoying thing is that it's not throwing anything. Just a blank page...
borisCallens
I also now noticed that the views are literly copied into my bin folder.
borisCallens
in your assembly does it have the aspx, aspx.cs and aspx.designer.cs? you should only really need the aspx.cs file in your assembly.
David Liddle
A: 

How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.

I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).

Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.

This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.

I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.

GuyIncognito
borisCallens
I didn't "add=>new" I just had three site projects having the same view. Because I didn't figure out how to get views in a seperate project yet I kept on maintaining all three of them. With the expected problems of human negligance... So this was actually another "that's it, I'm centralizing this thing" moment.
borisCallens
Also, I just dragged the views from one project to the library. But it seems it's not that ideal. The IDE doesn't recognise it as a view anymore so no more IDE suport vor the Html and Model properties :(
borisCallens
A: 

What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:

<%@ Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>

Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:

<add namespace="CommonProject.Web.Shared.Views.Search"/>
Lance Fisher
That's a check. But I didn't include the namespace because I want my inheriting views to be named the same so I need the namespace directive completely anyway.
borisCallens