Serve javascript file via http Handler

I’ve written a HTTP Handler that outputs content depending on values passed to the handler. I am trying to extend it so that it outputs flash aswell, however to do so I need to also serve a javascript file. The javascript file itself is an embedded resource in another assembly, so I am trying the following:

    public void ProcessRequest(HttpContext context) {

        ((System.Web.UI.Page)context.CurrentHandler).ClientScript.RegisterClientScriptInclude("swfobject", ((System.Web.UI.Page)context.CurrentHandler).ClientScript.GetWebResourceUrl(typeof(MyAssembly.Load), "MyResourceAssembly.swfobject.js"));

        context.Response.Write("Hello world");

    }

However, I am getting the error:

Unable to cast object of type ‘Handlers.GenericContentHandler’ to type ‘System.Web.UI.Page’.

Is there a way of acheiving this? I cant use a standard src=/swfobject.js because the script file is an embedded resource.

Or would a module be a better implementation?

Thanks in advance
higgsy

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You get InvalidCastException because your are trying to convert your current handler (ashx) to the page type.

I am not certain what you want to return in flash case – I am assuming that you probably want to return an html page with flash object embedded within and with js file included in header. So you need to emit such html where you would include link to the js file. Now, as you don’t have js on file system, your link should point to yet another handler that would extract the js code from embedded assembly and return it. Luckily such handler is already available (WebResourceLoader) and you can use it below to get the needed url:

(new Page()).ClientScript.GetWebResourceUrl(typeof(MyAssembly.Load), "MyResourceAssembly.swfobject.js");

Trick is that because you don’t have client script manager in generic handler, you simply create a new page instance and use that.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x