Quantcast
Channel: Brian Mains Blog
Viewing all articles
Browse latest Browse all 15

MVC and GetWebResourceUrl

$
0
0

Recently, Rick Strahl blogged about his issues with MVC and using some of the essential methods were available in ASP.NET web forms development, considerably the GetWebResourceUrl method (as detailed here: http://west-wind.com/weblog/posts/842507.aspx).  The GetWebResourceUrl method is a useful method to reference files (CSS, JS, images, etc.) stored as embedded resources in an assembly.  In web forms originally, using the statement Page.ClientScript.GetWebResourceUrl would handle creating these URL's.

Rick notes that in your MVC views, you can continue to use some of these methods, but there are issues with using them because of the lack of the same page lifecycle you have in web forms, and how you can create a custom component to fill this void.  I would like to illustrate another possible option if you are looking to accomplish this within a controller.

But first, I have to take you back to ASP.NET web forms (my apologizes to those strongly in the MVC camp).  Web pages could have direct access to this ClientScript property (of type ClientScriptManager) to use GetWebResourceUrl, and so there weren't any accessibility issues within the page.  Within custom components, the HttpContext object gives you access to the page via the Handler property, which is the current handler processing the request (in case of ASPX files, this is the Page class).  In any component, it's possible to get a reference to the page (provided the page is executing the request) using:

Page page = (Page)HttpContext.Current.Handler;

Now that the page instance is available, an embedded resource can be retrieved using:

page.ClientScript.GetWebResourceUrl(page.GetType(), "<resource name>");

Again, this would be in some sort of component or outside the realm of the page; if in an ASP.NET page, user control, or custom control (or anything else that inherits from Control), you can reference the page directly via the page property.

Within MVC, there are several options.  The ViewPage class inherits from page, but as previously mentioned, there may be issues with this here.  However, the controller can also reference the page.  While not directly being able to access the page through a Page property, it can using a similar approach as mentioned above.

The controller class has a HttpContext property, of type HttpContextBase.  This class has a CurrentHandler property, which is a reference to the Page.  Through this, you can cast it to Page and call the GetWebResourceUrl method as shown below:

var url = ((Page)HttpContext.CurrentHandler).ClientScript.GetWebResourceUrl(this.GetType(), "<resource name>");

To get the URL from the controller to the view, you have to return it via the action result or assign it to the ViewData collection, so there are a few options.  Note I'm still learning MVC, so there may be other areas to do this, and there may be other caveats.


Viewing all articles
Browse latest Browse all 15

Trending Articles