What different customizations are possible by using HttpHandlers in an ASP.NET application? - asp.net

Digging deeper into HttpHandlers I found they provide nice way to customize an ASP.NET application. I am new to ASP.NET and I want to know about different customizations that are possible using HttpHandlers. Lots of websites talk about how they are implemented but it would be nice to know some use cases beyond what ASP.NET already provides using HttpHandlers.

An ASPX page provides a base template (so to speak) for a form-based web page. By default, it outputs text/html and allows for easy adding of form elements and event handling for these elements.
In contrast, an HttpHandler is stripped to the bone. It is like a blank slate for HTTP requests. Therefore, an HttpHandler is good for many types of requests that do not necessarily require a web form. You could use an HttpHandler to output dynamic images, JSON, or many other MIME type results.
A couple examples:
1) You have a page which needs to make an AJAX call which will return a JSON response. An HttpHandler could be setup to handle this request and output the JSON.
2) You have a page which links to PDF documents that are stored as binary blobs in a database. An HttpHandler could be setup to handle this request and output the binary blob as a byte stream with a PDF MIME type for the content type.

Check this page for a good example and code of why you might want to customize them: http://dotnetslackers.com/articles/aspnet/Range-Specific-Requests-in-ASP-NET.aspx Essentially it can be used when you want to server certain files but not allow them to be accessible via a plain url (security).

Related

HttpHandler with a different extension to .ashx

I've got an image handler on my site which works on the url below. Obviously with being an asp.net httphandler it has the .ashx extension, even though it returns content with mime type image/jpeg
https://xxxxx/image.ashx?id=78164&imagelibraryid=0404fd0c-e681-4a5c-9899-f2709aaf7e0c
We want to use cloudflare for caching and serving images, but for it to work properly you have to use image urls with standard extensions i.e. jpg/gif/png etc.
Is there anyway my httphandler can detect and process any requests to image.jpeg?
Only image.jpeg should be handled by the httphandler, any other XXX.jpeg would still need to be served from the filesystem.
Is it possible?
Thanks
I think you have three options to allow you to serve the same images with a new url that looks like:
https://xxxxx/something-custom/images/78164/0404fd0c-e681-4a5c-9899-f2709aaf7e0c.jpg
Option one - use IIS rewrite rules to serve publically on one url that has proper extensions and rewrite the address inside the webserver so that the fragments get passed into the ashx handler.
Option two - use aspnet routing, originally designed for MVC, with a bunch of plumbing code to pass the data between the two paradigms, as detailed in this SO question
Option three - rewrite the code by moving it out of ashx into a new api controller as ashx handlers are a bit dated these days and mvc and model binding are often easier and more testable. Most of the core logic can probably be done quickly with lift and shift, and just use routing and mvc to map the new url fragments into variables consumed by your business logic.

How to call a local HTTP Handler

I've got a HTTPHandler which returns either an image or some text and want to call it from the local web-app.
One way to do it would be to create a WebClient and point it at the handler and download the data that way but this will have problems when authentication is turned on in production and also doesn't seem that efficient. Is there a better way to do this?
The reason I'm trying to do this is that I have a page which injects content from other sources into panels, some of the resources are local (to the web-app) and others are remote, I wanted one solution to cover every possibility so I can change the source fairly easily.
One of the simplest way is to link the content i.e. use html elements such as image, Iframe and point their source to the handler in question. It will work with local as well as remote handlers as long as URLs are accessible from client/browser.
If you must get the content for some kind of processing etc, then you may use WebClient to download the content - it anyway support HTTP based authentication schemes (including windows/integrated authentication) so it shouldn't be an issue.
If you want to optimize the process for local handlers then you probably need to abstract the interface for content provider - you can have two implementations (one that use WebClient to get remote content and other that will probably use the native API of local handler class directly to get the content).
Extract the code from your handler to a separate assembly
Reference this assembly in the project that has HTTP Handler and make the handler just call your shared code
Reference this assembly in your web app project.
You might want to create a provider for your web app that will either use an assembly in-proc or go to a remote site.

Google Sitemap HttpHandler cacheing

I have a HttpHandler that generates a Google sitemap based on my asp.net web.sitemap. Fairly standard stuff. Except that it does some fairly heavy database work to auto-generate additional urls for Ajax tabs within pages.
All this means our DB gets hit fairly heavily if the bot hits sitemap.axd.
What we need, of course, is output caching. But how do you go about caching inside something that basically writes directly to a XmlTextWriter?
The simplest answer is to write the XML to a string and store it in a static field.

Where content based websites store their content?

Sites like cnn.com or foxnews.com.
Where do they store all the articles? In html files? In database?
More logically to store everything in DB but how to generate a static link to something that is inside DB?
It's not that they have a a dynamic page load like: LoadArticle.aspx?ArticleID=123, every article has it's own address.
Please explain how this is done.
They use a special content management library called VoodooLib.dll.
Seriously, when you write something to a database, you normally generate some kind of unique identifier - 123, for example. It gets permanently associated with that record (article content). After that it is used to generate the same id as part of an Url at any time later.
As for the static link, it is a simple matter of Url Rewriting.
You generate static links to display on a page because they work much better for SEO. When a request for that static Url hits the server, it gets substituted for something "server friendly" and then gets to be processed.
They probably use some form of Content Management System (CMS). There are many different ones out there - most store the actual content in a database or as XML (some store XML in a database). They will the either publish that content as static HTML pages or, more commonly now, as dynamic pages that are cached. Many use what are known as "friendly URLs" that are virtual addresses that are mapped to the actual physical file path using URL-rewriting techniques.
Note you can't tell whether a page is dynamic or static simply from the extension. It is quite possible to have dynamic pages that end in the .html extension.
Just because the URL looks "static" doesn't mean it is; they could be using something like mod_rewrite or an IIS ISAPI to make the URLs more search engine friendly.
For the high-volume news sites that you mention, however, they may very well generate the pages statically in order to prevent overloading the database with repeated requests for the same article.
Look at the URl of this page, it doesn't have xxx.aspx?some-query-string
You are refering to using friendly URLs.
To do something like that, one common way is to use URL Rewrite and/or some custom HTTPModule
Here's a good reference: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Just because a page has a normal URL does not mean that it isn't serving dynamic content. With the Apache mod_rewrite module, it is possible to manipulate URLs. So, for example, a page like http://www.domain.tld/permalink/12345/message-title-slug can be converted internally to http://www.domain.tld/permalink/index.php?id=12345&slug=message-title-slug.
I do not know exactly what cnn.com and foxnews.com use, but I would bet that they use a Content Management System (CMS) which serves all pages dynamically, with the content stored either in a database or on the filesystem, and with authoring/publishing all being performed through the particular CMS.
Just checking cnn.com, the article links have in them
Year
Location (US or WORLD/specificlocationid)
Month
Day
Article name.
All of this information together can be used to uniquely identify any article (even less of it is probably actually needed). The dynamic content loading page address could easily be hidden by some method of URL rewriting, and then the information in the requested URL is used to determine which article in the DB is to be served up.
I don't know why all the other answerers seem to assume that some form of URL rewriting is necessary to create friendly URLs. It's not true at all.
It's perfectly possible to write web serving code that splits a URL into parameters - eg year, month, title - and pass that directly to the code that gets the content from the database, without any need to rewrite the URL. Most modern web frameworks such as Django and Rails include this functionality out of the box.
This is done through mod-rewrite techniques.
Here's an article about the mod rewriting engine: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
And here's their "guide": http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
I hope that helps. It should make for a good starting point. Goodluck.

What is the .MSPX file extension?

I've noticed a lot of Microsoft sites have the *.MSPX extension. While I'm very familiar with ASP.NET, I've not seen this extension before.
Does anyone know what this identifies?
A few internet searches led me to http://www.microsoft.com/backstage/bkst_column_46.mspx, but it was a dead link. Fortunately, it was archived on the Wayback Machine and you can read it here:
http://web.archive.org/web/20040803120105/http://www.microsoft.com/backstage/bkst_column_46.mspx
The .MSPX extension is part of the "Microsoft Network Project," which according to the article above, is designed to give Microsoft's sites a consistent look-and-feel worldwide, as well as keep the design of the site seperate from the content. Here's the gist of the article:
The presentation framework includes a custom Web handler built in ASP.NET. Pages that use the presentation framework have the .mspx filename extension, which is registered in Microsoft Internet Information Services (IIS) on the Web servers. When one of the Microsoft.com Web servers receives a request for an .mspx page, this custom Web handler intercepts that call and passes it to the framework for processing.
The framework first checks to see whether the result is cached. If it is, the page is rendered immediately. If the page is not cached, the handler looks up the URL for that page in the table of contents provided by the site owner (see below) to determine where the XML content for the page is stored. The framework then checks to see if the XML is cached, and either returns the cached content or retrieves the XML from the data store identified in the table of contents file.
Within the file that holds the content for the page, XML tags identify the content template to be used. The framework retrieves the appropriate template and uses a series of XSLTs to assemble the page, including the masthead, the footer, and the primary navigational column, finally rendering the content within the content pane.
I think it's an XML based template system that outputs HTML. I think it's internal to MS only.
Well, a little googling found this:
The presentation framework includes a
custom Web handler built in ASP.NET.
Pages that use the presentation
framework have the .mspx filename
extension, which is registered in
Microsoft Internet Information
Services (IIS) on the Web servers.
When one of the Microsoft.com Web
servers receives a request for an
.mspx page, this custom Web handler
intercepts that call and passes it to
the framework for processing."
I'd like to find out more info though.
I love you guys, i was asking myself also many times, why MS uses .mspx and what it is at all?! :)
That time i couldn´t find any informations quickly and assumed it would just be something on top of asp.net or maybe not even that, because you should be able to assign the same asp.net cgi dll to .mspx also easy too ;)
But, surely, it can be anything.. also an "special" CGI itself (completely beside ASP.NET), which processes that request with much better / much more cache-use, easier editing and so on..
The end of the story was, that i came accross the view, that maybe it´s not important to know, what .mspx exactly is :)

Resources