Which one to use Http Handler or Http Module - asp.net

We want to do something like we have to execute some piece of code in each request to the application. We want to use this same code in multiple applications.
What this code will do is, this will check the incoming request and according to some conditions it will decide whether it has to redirect or not.
So while searching i found that we can use either http handler or http module. But i am not sure about which one has to chose in this case? Please give your suggestions.

HttpModule in this case. It sits in the pipeline where you can inspect each and every request.
How To Create an ASP.NET HTTP Module Using Visual C# .NET
http://support.microsoft.com/kb/307996
HttpHandler is altogether different thing. If you implement HttpHandler for existing file types such as .aspx etc, you will have implement what is already implemented by ASP.NET runtime which is beyond the scope of your requirement.

Related

When and how to use POST, PUT, and Delete method in Web API

I am new to Web API and the tutorial I was watching uses Fiddler to sniff and send request.
My question is how to use the post, put and delete methods without using fiddler and where are these usually used in the when creating in software development? Fiddler allows you to compose a request body how about the browser though?
I don't seem to understand why you are only able to use these on a sniffing tool. I understand that I am most probably speaking out of ignorance.
(This answer is not specific to ASP.NET)
To avoid any confusion, I'm assuming you understand this much about RESTful webservices:
If you are creating a new entity, use POST.
If you are reading an entity, use GET.
If you are updating an entity, use PUT.
If you are deleting an entity, use DELETE.
As you noticed, only the GET and POST methods can be used in plain web forms. AJAX, however, supports using any HTTP method. Therefore, to use the PUT and DELETE methods, send your data to the server using JavaScript methods such as XMLHttpRequest, the newer Fetch API or a convenient wrapper like jQuery's $.ajax. (There are many tutorials for whichever you pick.) For example, if you have a form of updated values, replace your input button with a button element that has a JavaScript click event handler. The same principle applies to a delete link, swap the href for a click event handler that makes an AJAX request to your server.
If you simply want to create requests using some tool, then you can use tools like Fiddler, Postman, curl, etc. - as you have stated, Fiddler can also be used to monitor as well as create requests.
If you want to go beyond that,
you have HttpClient, WebClient to build requests on your own, and for testing.
If you want to get "full control" then look into HttpWebRequest.
VS debugging tools will allow you to inspect the full flow - from request to response - using things like breakpoints, watch, etc.
See previous/other answers if you want to build/create requests client-side (Javascript / XMLHttpRequest)
Hth.

Proper way to abort a web-service request in ASP.NET?

We have various web-services which accept requests, but there are periods in the day for some of the services where we do not want to accept requests. (I won't go into why at the moment but it is a requirement).
In these cases I'd like to abort the request as it is being received and am wondering the best way to do this and where is the most appropriate place in the ASP.NET pipeline.
I am currently returning back a status of ServiceUnavailable in the BeginRequest in the global.aspx.
Is the the correct way to implement this type of functionality or is there a better alternative. (_webState is an internal variable we use for the current state of the service)
if (_webState != WebStates.Runnable)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.ServiceUnavailable;
Response.Write("<p>The Service is not available to accept requests</p>");
Response.End();
}
I think the correct way is to create a custom HTTP module as an extension of ASP.NET request handling pipeline. In this module you will be able to refuse connections according to needed strategy. ServiceUnavailable is a standard way of going, we've done this in same way.
But here is exist one very big "but" =). Going this way you can disable only requests that is targeted to ASP.NET application, if you need to refuse all request made to particular application you need to write your custom tool that will stop corresponding ApplicatonPool.

Simple server and client request/response in C#

OK. I just want to know if this can be done.
I'm using C# asp.net language.
I want to send a structure to a webserver from a webpage (much like you you can pass a structure to a function). The server would act on the data found in the structure, and then return back the structure (with its contents modified) to the "client" webpage.
The webpage would then, based on the data received, do a particular task.
is it possible that information be passed between webpages & servers just like functions?
If not, is there any way that i may be able to accomplish what was said above?
Answer is yes, you can try calling a server-side function from a client-side script.
Here are a few good reads:
Client-Side Web Service Calls with AJAX Extensions
Client Side Callbacks in ASP.NET 2.0
Extend ASP.NET AJAX Client-Side Function - The Server-Side Way
ASP.NET 2.0 Client-Side Features: Bring Server-Side Power to the Client
Sure. You can encode your structure in a string, and then send it to the server as a query string or in a form POST, using either a dedicated link or plain JavaScript or Ajax (XMLHTTP). When the response arrives, you can similarly decode it with JavaScript and then take whatever action is appropriate.
There are several libraries to help automate this kind of thing, including XML-RPC.

IIS Integrated Request Processing Pipeline -- Modify Request

I want to implement an ISAPI filter like feature using HttpModule in IIS7 running under IIS Integrated Request Processing Pipeline mode.
The goal is to look at the incoming request at the Web Server level, and inject some custom HttpHeaders into the request. (for ex: HTTP\_EAUTH\_ID)
And later in the page lifecycle of an ASPX page, i should be able to use that variable as
string eauthId = Request.ServerVariables["HTTP\_EAUTH\_ID"].ToString();
So implementing this module at the Web Server level, is it possible to alter the ServerVariables collection ??
HttpRequest.ServerVariables Property is a read-only collection. So, you cannot directly modify that. I would suggest storing your custom data in httpcontext (or global application object or your database) from your httpmodule and then reading that shared value in the aspx page.
If you still want to modify server variables, there is a hack technique mentioned in this thread using Reflection.
I believe the server variables list only contains the headers sent from the browser to the server.
You won't be able to modify either the HttpRequest.Headers or the HttpRequest.ServerVariables collection. You will however be able to tack on your information to any of:
HttpContext.Current.Items
HttpContext.Current.Response.Headers
Unfortunately, Request.Params, Request.QueryString, Request.Cookies, Request.Form (and almost any other place you'd think of stuffing it is read only.
I'd strongly advise against using reflection if this is a HttpModule you're planning on installing into IIS 7. Given that this code will be called for (potentially) every request that goes through the web server it'll need to be really fast and reflection just isn't going to cut it (unless you have very few users).
Good luck!

Can I check if a SOAP web service supports a certain WebMethod?

Our web services are distributed across different servers for various reasons (such as decreasing latency to the client), and they're not always all up-to-date. Rather than throwing an exception when a method doesn't exist because the particular web service is too old, it would be nicer if we could have the client check if the service responds to a given method before calling it, and otherwise disable the feature (or work around it).
Is there a way to do that?
Get the WSDL (append ?wsdl to the URL) - you can parse that any way you like.
Unit test the web service to ensure its signatures don't break. When you write code that breaks the method signature, you'll know and can adjust the other applications accordingly.
Or just don't break the web services and publish them in a way that enable syou to version them. As in http://services.domain.com/MyService/V1.1/Service.asmx (for .NET) so that way your applications that use v1.1 won't break when you publish v1.2 and make breaking changes.
I would also check out using an internal UDDI server if it's really that big of a hasle to manage your web services. Using the Green Pages of UDDI will tell you what you want to know about the service.
When you are making a SOAP request you are just sending an HTTP request to a server. If the server understands it, it will respond with an HTTP 200 and some XML back, if it doesn't it will send you some error HTTP code (404, 500, ...)
There is no general way to ask for the existance of a "method" exposed by a web service. Try to use the WSDL exposed if it is automatic, or just try to use the "method" and check for an error in the response (you don't have to send an exception to the user...)
Also, I don't know if I understood you well, but you are thinking of quering the server twice, once to check if the method exists, and second to make the actual call it if it does? I would just check for the error if it doesn't, and proceed normally if it does.

Resources