ihttphandler to write out file exactly as is
I am writing a class that inherits from IHttpHandler for script and css combining. I onl开发者_开发知识库y want to combine if the querystring has a special parameter defined. If this parameter is not defined then I want to write the contents of the file as if the handler wasn't even involved. What is the best way to deliver the file unharmed?
EIDT:
The one problem I'm encountering is that I have a script tag on the page that refers to a script in a virtual directory but the page I am on is in a subdirectory of the application.
The page that the control script is being referenced from is located at webserver/Admin/Default.aspx. When I access the Request object in the class that implements IHttpHandler all file path properties are the following: webserver/Admin/~/SharedScripts/control.js. How do I resolve this?
You can check the query string parameter in the 'ProcessRequest(HttpContext context)' method like this:
context.Request.QueryString["paramertername"]
If you want to stream the requested file as is you can then do the following:
string physicalFilePath = context.Request.PhysicalPath;
string fileContent = string.Empty;
// Determine whether file exists
if (File.Exists(physicalFilePath))
{
// Read content from file
using (StreamReader streamReader = File.OpenText(physicalFilePath))
{
fileContent = streamReader.ReadToEnd();
}
}
context.Response.Output.Write(convertedFile);
context.Response.Flush();
PS: You can also check out the following code project article for a more comprehensive example: http://www.codeproject.com/KB/locale/LocalizedScriptsAndStyles.aspx
精彩评论