I'm migrating some code from the ASPX view engine to Razor and I've run up onto a roadblock.
I have this code:
<link rel="Stylesheet" type="text/css" href="
<%=Page.ClientScript.GetWebResourceUrl
(typeof(DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector),
"DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector.css")%>" />
The problem here is that with Razor, I have no Page property.
So I took a step back for a second, and I'm looking at this wondering: What is the right way to get embedded resources in Razor?
I've spent a good bit of time trying to find solutions on this subject, but I haven't really found anything other than "wrap a new Page in a helper"
Is that the only way to do it? Or is there something more correct?
Beside invoking
new Page()...
you can directly call the underlying implementation. Place this code in some static class:And in the Razor view or code behind use:
Of course, using WebResource URLs in Razor views is not very useful. Instead it is recommended to place the resource into the Content or Scripts folder of your MVC application directly.
But the situation changes if you want to write
HtmlHelper
functions in shared class libraries which cannot place content into the target project.Rationale
This approach basically avoids to create a new, bulky
Page
object on each invocation ofGetWebResourceUrl
.Looking at the reference source code it turns out that the
Page
andScriptManager
context is used just for nothing. So invokingAssemblyResourceLoader.GetWebResourceUrlInternal
directly will hit the nail on the head. All you need to create a working WebResource.axd URL is the Assembly and, of course, the resource name.The drawback is that this function is internal, so it have to be invoked by reflection. However, the above implementation avoids the overhead of calling the function by reflection every time. Instead
CreateDelegate
is used once to get an ordinary delegate which can be called almost without overhead.The race condition at the access of field
GetWebResourceUrlInternal
is intended. It does not cause any significant harm because it is very unlikely to run into this code fragment with many parallel threads at the first invocation, and even if it happens the result is still reliable.Unfortunately the web resources are quite tied to the webforms infrastructure and it is difficult to reuse them without it. So a bit hacky but you could write a helper:
and in your razor view:
Another possibility is to write a custom HTTP handler/controller which will read the embedded resource from the assembly and stream it to the response by setting the proper content type.