Using Url.Resource() in a portable area

707 views
Skip to first unread message

jonathan matheus

unread,
Dec 2, 2010, 12:22:39 PM12/2/10
to mvccontrib-discuss
I'd like to embed javascript & images into my portable area &
reference it within an embedded view using Url.Resource(). However,
I'm not able to get it working. It creates a url that results in a
404. I've put my portable area details below. Am I missing something?

Here's what I have

Portabl Area Assembly (Registration.Web):
Content\Images\logo.gif
Views\Register\Index.cshtml
UserRegistrationRegistration.cs

UserRegistrationRegistration.cs:

public class UserRegistrationRegistration : PortableAreaRegistration
{
public override void RegisterArea(AreaRegistrationContext
context, IApplicationBus bus)
{
context.MapRoute(
"Register",
"UserRegistration",
new { controller = "Register", action = "index" });

context.MapRoute(
"GenericRegistration",
"UserRegistration/{controller}/{action}",
new { controller = "Register", action = "index" });

RegisterAreaEmbeddedResources();
}

public override string AreaName
{
get { return "UserRegistration"; }
}
}


Views\Register\Index.cshtml:

@using MvcContrib

@{
View.Title = "Register";
}

<img src="@Url.Resource("Content.Images.logo.gif")" height="100px"
width="100px" />

The url it points to is:
/UserRegistration/EmbeddedResource?
resourceName=Content.Images.logo.gif


Thanks,
Jonathan

Jonathan Matheus

unread,
Dec 2, 2010, 1:03:04 PM12/2/10
to mvccontrib-discuss
I found the CreateStaticResourceRoute() method on the PortableAreaRegistration class. After looking in refelctor, it looks like I could do the following:

UserRegistration.cs:
CreateStaticResourceRoute(context, "Content/Images");
Views\Register\Index.cshtml:

@using MvcContrib

@{
   View.Title = "Register";
}

<img src="~/UserRegistration/Content/Images/logo.gif" height="100px"width="100px"  />

This would be way better than the Url.Resource extension method. However, it also returns a 404.

Jonathan

johncoder

unread,
Dec 2, 2010, 2:26:51 PM12/2/10
to mvccontrib-discuss
Is it because you have a ~ in your src path?

On Dec 2, 1:03 pm, Jonathan Matheus <jmath...@gmail.com> wrote:
> I found the CreateStaticResourceRoute() method on the
> PortableAreaRegistration class. After looking in refelctor, it looks like I
> could do the following:
>
> UserRegistration.cs:
>
> CreateStaticResourceRoute(context, "Content/Images");
>
> Views\Register\Index.cshtml:
>
> @using MvcContrib
>
> @{
>    View.Title = "Register";
>
> }
>
> <img src="~/UserRegistration/Content/Images/logo.gif"
> height="100px"width="100px"  />
>
> This would be way better than the Url.Resource extension method. However, it
> also returns a 404.
>
> Jonathan
>

Jonathan Matheus

unread,
Dec 2, 2010, 2:29:31 PM12/2/10
to mvccontri...@googlegroups.com
Nope,

Tried that first

> --
> Contact Jeffrey Palermo or Eric Hexter with specific questions about the MvcContrib project. Or go to http://mvccontrib.org
>
> To unsubscribe from this group, send email to mvccontrib-disc...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/mvccontrib-discuss?hl=en

Jonathan Matheus

unread,
Dec 7, 2010, 8:59:32 AM12/7/10
to Jonathan Matheus, mvccontri...@googlegroups.com
I'm still stuck on this issue. Does anyone have any suggestions?

Jeremy Skinner

unread,
Dec 7, 2010, 9:16:43 AM12/7/10
to mvccontri...@googlegroups.com
Out of the box, portable areas can automatically register routes for common static resources - one for Images, one for Styles and one for Scripts. These are registered through the RegisterDefaultRoutes method in the PortableAreaRegistration class. However, by overriding RegisterArea these won't be automatically registered. You should either call RegisterDefaultRoutes(context) or simply call base.RegisterArea. You can check if these routes have been successfully registered by inspecting RouteTable.Routes at runtime.

These routes will look for embedded resources in the following directories within the portable area assembly and map to the following URLs:

directory /Areas/<AreaName>/Content/Images maps to the URL /<AreaName>/Images
directory /Areas/<AreaName>/Content/Styles  maps to URL /<AreaName>/Styles
directory /Areas/<AreaName>/Content/Scripts maps to URL /<AreaName>/Scripts

Now, you can reference the resources from within your view. You don't need to use Url.Resource for this, as these are well-known static routes - Url.Resource is useful for when you have custom embedded resources, but in this case you can just treat JS/images/css as if they were part of your app, eg:

<img src="@Url.Content("~/AreaName/Images/foo.jpg")" />

If you get stuck, I'd suggest looking at the code for the OpenIdPortableArea at http://openidportablearea.codeplex.com/ - the code for this includes an area with static resources and also a consuming project that illustrates how to consume the area correctly. 

Jeremy 

Eric Hexter

unread,
Dec 7, 2010, 9:20:03 AM12/7/10
to mvccontri...@googlegroups.com

If you can zip up the smallest sample which demonstrares the problem ill look at it.

sent from my mobile

Jonathan Matheus

unread,
Dec 8, 2010, 12:50:57 AM12/8/10
to mvccontri...@googlegroups.com
It was totally my fault. I had a more generic route mapped before calling CreateStaticResourceRoute. Thanks for the info about the default routes. That's way simpler.

Jonathan
Reply all
Reply to author
Forward
0 new messages