Configure IHttpHandler in the Test Web Server - asp.net

I am trying to implement an IHttpHandler. I have defined an appropriate class but the debug web server (you know, the one you get if you hit f5 in Visual Studio) is responding with "Can't Display Page".
I looked here http://msdn.microsoft.com/en-us/library/ms228090%28v=VS.90%29.aspx to learn how to configure the handler, and it seems there are different ways for IIS6 and 7. But the process is put something in the web.config and then set it up in IIS Manager. However, that is a deployment issue. I want to be able to run it in the test server, and I don't know how to do this second step in the test server.
I put the following in my web.config:
<httpHandlers>
<add verb="*" path="*.sample"
type="MyNamespace.Code.HelloWorldHandler"/>
</httpHandlers>
HelloWorldHandler is the code from the link above (wrapped in MyNamespace.)
Can someone let me know how to configure this correctly for the development server?

You should be able to set the web server settings via web.config like this...
<configuration>
<system.webServer>
<handlers>
<add name="HelloWorldHandler"
verb="*"
path="*.sample"
type="MyNamespace.Code.HelloWorldHandler"
resourceType="Unspecified" />
<handlers>
</system.webServer>
</configuration>

Related

ServiceStack and SignalR together in same project

It is somewhat trivial question, but I am using SignalR and ServiceStack in single Asp.Net host application.
Means, it is simple Asp.Net blank application, ServiceStack is running on / and it is showing default page using Razor. Running perfectly.
Now, I added SignalR asp.net host. Added startup class and created hub to listen and broadcast chat message.
I have wrote client code in default page only. Now, things are working fine. Means, API and SignalR are both running on local machine.
Now, the question is, is this the right way of doing things? Means, are there two different processes hitting IIS. Or is there any way I can chain process to single process.
Or even part of ServiceStack API I can make real-time.
Please let me know if any further information is required.
Yes its possible, but you cannot run ServiceStack on Owin at the moment (as far as I know)
So you need to run ServiceStack in a specific location.
<location path="ssapi">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*"/>
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
</location>
You may need to remove the handlers for owin/signalr stuff from the location too.
Alternatively you can setup SignalR on a specific path and remove ServiceStack from that path.
i.e
<location path="signalr">
<system.web>
<httpHandlers>
<remove type="ServiceStack.HttpHandlerFactory, ServiceStack" />
</httpHandlers>
</system.web>
<system.webServer>
<handlers>
<remove name="ServiceStack.Factory" />
</handlers>
</system.webServer>
</location>
I know the question focused on SignalR, but ServiceStack added support for Server-Sent Events in v4.0.31. Basicaly, this is a very long-lived HTTP request that streams to the client, allowing for real-time server "push"
This feature is supported by most modern browsers, and polyfills can be used to support IE back to 8. ServiceStack also provides a C# client.

ASHX handler return 500 Internal server error

I have the ASP.NET 4 asmx web service (IIS 7.5). It works by https. I added ashx handler. It works locally, but do not work at the hosting. Return 500 Internal Server Error. What to do?
I encountered this problem when I changed the application pool in IIS from classic to integrated. I solved it by adding a handler to the system.webServer of the web.config file.
<add verb="*" path="*.ashx" name="ImageFromDB" type="ImageFromDB" />
like this:
<system.webServer>
<handlers>
<add verb="*" path="*.ashx" name="ImageFromDB" type="ImageFromDB" />
</handlers>
</system.webServer>
This added 'ImageFromDB' to the HandlerMappings in IIS.
This link was very helpful in pointing me in the right direction.
ASP.Net will display a 500 if you don't have the customErrors property set to anything or it is set to On.
Add this to the web.config to see what the actual error is:
<customErrors mode="Off" />
Once you know what the actual error is, you can proceed to fix it.

Set handler for PDF in IIS

I developed an HttpHandler in order to count number of downloads for PDF files.
Now the problem is: How can I make IIS use my handler for PDF files?
For IIS 7 it does depend on which mode you are running IIS in. Microsoft has a great tutorial on this How to: Register HTTP Handlers which goes over all the different configuration scenarios but assuming you are running IIS 7 in integrated mode and your handler is a compiled binary you would need a web.config entry similar to the following:
<configuration>
<system.webServer>
<handlers>
<add name="pdfCountHandler" verb="*"
path="*.pdf"
type="<your handler class name>, <your handler assembly name>"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>

HttpHandler not being called

I need to write a HttpHandler that will serve up JavaScript files which are embedded resources in .DLLs in my project. A reference in a view cannot directly see such a resource, so I planned to use a HttpHandler module that would intercept any request with a path /js/[file] , find a matching embedded file and return the script.
The problem is that my HttpHandler code is never called, despite trying lots of different settings in the section of web.config. I'm obviously missing something but with no error messages I cannot see what that is. All I ever get is a 404 from the static file handler.
Q1) Am I missing something obvious?
Q2) Is there a way to get IIS to tell me why it is not calling my handler?
Summary: I am testing on IIS Express (v8) for an ASP.NET MVC 4 application.
I created a simple library that implements IHttpHandler and added a reference to this in my test MVC application, and the following lines in web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="true" />
<handlers>
<add name="ejs" path="js/*" verb="*" type="EmbeddedJsHandler.EmbeddedJsHandler, EmbeddedJsHandler" preCondition="integratedMode" />
The library is there, but it is never called. Any request with /js/test.js or whatever just results in a 404 error.
So far I've tried lots of different configurations and settings in the handler code. I've tried preCondition, resourceType="Unspecified", modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
I've tried paths:
js/*.js
js/*
js/*.*
I've checked the integrated mode settings section (in system.webServer) is being used, and confirmed it is.
I've searched stack overflow for similar cases, and tried many of the possible solutions.. still no joy.
Heck even Jon Skeet has these sort of problems!
Why isn't my IHttpHandler being called?
Finally figured it out by accident - it was a missing routes.IgnoreRoute() in the RouteConfig.cs file - the MVC routing engine wasn't configured to ignore this path, so was passing it to the static file handler.
Doh!
Check this:
How to: Register HTTP Handlers:
To register an HTTP handler for IIS 7.0 running in Integrated Mode:
Compile the HTTP handler class and copy the resulting assembly to the Bin folder under the application's root folder.
In the application's Web.config file, create a handlers element in the system.webServer section.
The following example shows how to register an HTTP handler that responds to requests for the SampleHandler.new resource. The handler is defined as the class SampleHandler in the assembly SampleHandlerAssembly.
<configuration>
<system.webServer>
<handlers>
<add name="SampleHandler" verb="*"
path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
Note: The resourceType attribute performs the same function as the Verify file exists option in IIS manager for IIS 6.0.
For IIS 7.0 running in Integrated mode, only the registration in the handlers element is required.
I cannot tell you directly why your handler isn't working, but I will give you an example of a handler we use and works for us:
<system.webServer>
<handlers>
<add name="JS handler" path="*.js" verb="*" type="Handlers.Minifiers.JSMinify" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>
We also have this segment, which is at least necessary for running in Cassini
<system.web>
<httpHandlers>
<add verb="*" path="*.js" type="Handlers.Minifiers.JSMinify" validate="false"/>
</httpHandlers>
</system.web>
If this doesn't help, have tou tried using path="/js/*"?

http handler asp.net

High I'm trying to get a http handler working in iis 7.5 on my local machine. In the mode on visual studio iis my handler works with the web config set to.
<httpHandlers>
<add verb="GET" path="ShowImages.ashx" type="achangeoftack_new_web.ShowImages" />
</httpHandlers>
but when deployed it throws errors so I've set it to
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<add name="ShowImages.ashx" verb="GET" path="~/ShowImages.ashx" type="achangeoftack_new_web.ShowImages" />
</handlers>
<validation validateIntegratedModeConfiguration="true" />
But I'm still getting no luck with the handler working. I think I've tried everything now I'm starting to pull my hair out.
If you're using an ASHX, you don't need to do the registration in your web.config, just use it like a page url e.g.
<img src="ShowImages.ashx?id=SomeImageId" />
and it'll just work. So I'd try taking those elements out of your web.config and see if that fixes your errors.
Why This Works
When you run aspnet_regiis on a machine, ASHX is one of the extensions that's registered into IIS for you.
At runtime when IIS receives a request for an ASHX file, it passes it across to ASP.NET and ASP.NET then resolves it in the same way as for a page or user control, and your code gets run.
You only need to register your handler in web.config if you've written a handler in a standalone class.

Resources