ASP.NET MVC 3, Razor Views, and Portable Areas
Asked Answered
M

3

16

I am trying to using portable views with ASP.NET MVC 3 and razor views as that seems like the best way to create an easy plug-in architecture. So I have my class library setup and I have my view located in /Views/Admin/Index.cshtml and it is set as an Embedded Resource. I then include that project as a dependency for the main web application project. When I try to access the Admin controller, Index action I get a message that is can't find that view file (so the controller is being properly included). I am assume it is trying to look in the main web application project and not the portable areas binary. Is there a way to get razor views to work with portable areas?

Merkle answered 18/4, 2011 at 3:29 Comment(0)
E
12

I have been struggling on this particular issue for a while, but I think I finally figured it out.

The folder structure and how the namespaces are called inside your project is very important for this to work properly!

I have a working example of a Portable Area with embedded razor views here:

https://github.com/fretje/MembershipStarterKit

Take a look at the structure of the project.

The area's name is UserAdministration, and there is a UserAdministrationRegistration class in the root of the project, which resides in the UserAdministration namespace. Then there's a Controllers, Models and Views folder (just like a normal MVC project) and under the Views folder, there's again a UserAdministration folder which contains the views for the area.

Also something else which is very important for the embedded views to work: you have to register a new view engine in the Application_Start method of your global.asax.cs file, did you do that?

PortableAreaRegistration.RegisterEmbeddedViewEngine();

And... In your registration class, make sure you override the RegisterArea method which takes 2 parameters (AreaRegistrationContext context and IApplicationBus bus), and call the base implementation in there:

public override void RegisterArea(AreaRegistrationContext context, 
    IApplicationBus bus)
{
    base.RegisterArea(context, bus); // <== very important!

    context.MapRoute(
        "UserAdministration", 
        AreaName + "/{controller}/{action}/{id}",
        new { controller = "UserAdministration", action = "Index", 
              id = UrlParameter.Optional }
    );
}

If you don't call the base implementation, you have to at least add a

RegisterAreaEmbeddedResources();

To make sure that your embedded views and resources are registered.

Eggleston answered 9/5, 2011 at 14:32 Comment(3)
Thanks for this answer, exactly what I was looking for. I still get the view was not found error. It is set as embedded but still nothing.Pinkney
I had to add a nuget package reference to EmbeddedResourceVirtualPathProvider in my website to get this to work.Pinkney
This looks identical to what I had. It works fine on my dev machine, but it doesn't actually look up the write view files once deployed. I logged the views it looked for, and it skipped the .cshtml files completely, though it did find the ones for _layout and _viewstart.Wretched
P
1

I got this working by following the instructions in Fretje's answer and then also add a nuget package reference to EmbeddedResourceVirtualPathProvider in your website.

Pinkney answered 3/10, 2011 at 20:26 Comment(2)
Just the reference? What did you do to hook it up?Wretched
In Visual Studio, right-click on references in Solution Explorer and select "Add Library Package Reference". Select "online" in the popup and search for "EmbeddedResourceVirtualPathProvider" then click install. Note that you need VS2010 that was updated to Service Pack 1 for Nuget package installer to be part of it.Pinkney
K
0

Did you make sure that you Marked your View as Embedded Resource in your Portable Area?

Also i found that nice feature of portable areas is that you can override the embedded Views, so if you Place a View in your Host App with Same name and Location of the Embedded one with Different Code Logic it will take priority over the Embedded one Nice !!!

Hope this Helps

Kirin answered 29/4, 2011 at 21:26 Comment(1)
I suppose that depends on the order of the registered view engines, if the embedded view engine is first the portable area views will override regular views in the root project. As a side note I will say you can sometimes get a small performance gain by clearing all your views engines and make sure you only have registered the ones you use in your project. See insightpassion.com/norbertraus/index.php/2011/04/29/…Ichinomiya

© 2022 - 2024 — McMap. All rights reserved.