This is what I have in the Application_BeginRequest function in my global.
if (context.Request.Path.IndexOf(@"/ViewHtml/") > 0 )//Check path to see if we are serving local files
{
string[] args = context.Request.Path.Split('/');
int lastArg = args.Count() - 1;//used to get last agrument from url file name
int secondLastArg = args.Count() - 2;//used to get second last argument from url entity ID
int thirdLastArgHistory = args.Count() - 3;//used to third last argument from url History file name
int fourthLastArgHistory = args.Count() - 4;//used to third last argument from url History file name
string fileNameFromUrl = args[lastArg];
string entityId = args[secondLastArg];//Getting entity id from this location in url, should be valid if requesting a html page
int Id = -1;
try { int.TryParse(entityId, out Id); }
catch { Id = -1; }
Entity contentToView = null;
EntityService srv = new EntityService();
if (Id != -1)
{
if (Id == 0)//Enitity Id didn't get valid Id from url, request must be for an item in an html page
{
//Get entity id from appropriate place in url when requesting an item for a page
entityId = args[thirdLastArgHistory];
try { int.TryParse(entityId, out Id); }
catch { Id = -1; }
}
if(Id != -1)
contentToView = srv.Get(Id);//Get content entity from parsed ID
}
//TODO check security to make sure logged in uer can see uploaded file
string filePath = "";//Used to hold actual file path of file being requested
if (context.Request.Path.IndexOf(@"/ViewHtml/FileHistory/") > 0)//Get html from files hisorty
{
if (string.Equals(fileNameFromUrl, contentToView.Name))
{
string historyFileName = args[thirdLastArgHistory];//Get the history file name
//Create path for html page
filePath = ConfigurationSettings.AppSettings["Library Folder"] + contentToView.ContentExt.UploadedFileName +
@"\History\" + historyFileName + @"\Html\" +
contentToView.ContentExt.UploadedFileName + ".html";
}
else//Create file path for item in page IE: jpg,gif, css, js
{
string historyFileName = args[fourthLastArgHistory];//Get the history file name
//Create files path from contentToView and "Library Files" path on server
filePath = ConfigurationSettings.AppSettings["Library Folder"] + contentToView.ContentExt.UploadedFileName + @"\History\"
+ historyFileName + @"\Html\" + args[secondLastArg] + @"\" + args[lastArg];
}
}
else//Get html for file
{
if (string.Equals(fileNameFromUrl, contentToView.Name))
{
//Create path for html page
filePath = ConfigurationSettings.AppSettings["Library Folder"] + contentToView.ContentExt.UploadedFileName
+ @"\Html\" + contentToView.ContentExt.UploadedFileName + ".html";
}
else//Create file path for item in page IE jpg,gif, css, js
{
//Create files path from contentToView and "Library Files" path on server
filePath = ConfigurationSettings.AppSettings["Library Folder"] + contentToView.ContentExt.UploadedFileName +
@"\Html\" + args[secondLastArg] + @"\" + args[lastArg];
}
}
byte[] img = null;//hold img bytes to send
string html = "";//hold html to send
FileInfo file = new FileInfo(filePath);
if (file.Exists)//
{
string type = GetContentTypeFromExt(file.Extension);
switch (type)
{
case "image/jpeg":
case "image/gif":
case "image/png":
img = File.ReadAllBytes(filePath);
context.Response.Clear();
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = type;
context.Response.BinaryWrite(img);
context.Response.End();
break;
case "text/html":
case "text/xml":
case "text/plain":
html = File.ReadAllText(filePath);
context.Response.Clear();
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = type;
context.Response.Write(html);
context.Response.End();
break;
default:
break;
}
}