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.
Related
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.
Is it possible to modify the the routes (and thus the RouteTable) outside of the global.asax file, maybe in a controller? Is this even advisable?
My reason for asking has to do with IIS 6 and Integrated Mode not allowing for Request context calls. I'm implementing internationalization for a site and keeping track of the culture in the URL. The culture is originally read from a .config file and loaded as a route default. This file read is what ends up throwing the error (another few steps up the stack). I based this off the method described here.
You can access the routing table pretty much anywhere like so System.Web.Routing.RouteTable.Routes, have tested this from a controller and it worked fine.
I am trying to use aspx pages as an email templates. There will likely be a bunch of objects on the page which will be used as replacements in the html. Because it's an aspx page I'll be able to use databinding, repeaters, etc. At run time, I want to be able to instantiate the aspx page from its path, pass in a bunch of properties, and then get the rendered result of the page and email it. This seems pretty straightforward from a asp.net website (maybe using BuildManager or Server.Execute.) However, I want to be able to use the same templates via a console application by just loading up a page object from its filepath. Is this possible?
You could host your own webserver. Like the Cassini webserver.
In my own application (a Windows-based Desktop-CMS), I include a web server, too (non-Cassini). It works very well, also it does not serve ASP.NET but plain, HTML.
As I did some research back then, I first wanted to use the Cassini, too, but at some point, I found out that too much user privileges were required to run it successfully; this may not be an issue to you, but keeping this in mind and try to run it early with the permissions of the later user, might be a good idea.
Is it possible to clear the output cache of one asp.net web application from inside another asp.net web application?
Reason being... We have several wep applications structured like...
http://www.website.com/intranet/cms/
http://www.website.com/area1/
http://www.website.com/area2/
Pages in /area1/ and /area2/ are cached and are managed through /intranet/cms/. When a page is edited using /intranet/cms/ I want to clear it out of the cache in the appropriate /area#/ application.
I already tried using a VaryByCustom that looks up a guid stored in the HttpContext.Cache but that seems to be cached per web application, that doesn't work.
Really if there were any way of passing data between web applications on a single server, that would solve my problem, since I can use that + VaryByCustom.
Thanks!
-Mike Thomas
The way I've done this in the past is to have a "hidden" page (in each of the /areaX sites) that does the flushing, reloading, etc. The page validates a shared secret query parameter before doing anything (to avoid DoS attacks). If valid the page would output an "OK" message once the operation is complete; generates a 404 error if the secret is invalid.
If you want the flush to be on a per-item or per-group basis then add a second parameter that identifies that item/group.
This method is also server technology independent, and can be triggered by other management tools if required.
One way I know of doing this is by using a shared resource as a dependency, usually a file. When the file is changed, the cache is cleared. I think you can use HttpResponse.AddFileDependency for this.
However, in these cases it's usually better to use an out-of-process cache such as memcached. I haven't tested it myself, but this link deals on using memcached with OutputCache.
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 :)