• HEXUS
  • HEXUS.tv
  • channel
  • gaming
  • lifestyle
  • trust
  • community
  • ESReality
  • HEXUS.community discussion forumsVisit Corsair.com

    Welcome to the HEXUS.community discussion forums forums.

    You are currently viewing our boards as a guest which gives you limited access to view most discussions and other features. By joining our free community you will have access to post topics, respond to polls and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development

    Software and web development Databases, graphics, programming, scripting and web development.

    Reply
     
    LinkBack Thread Tools
    Old 30-05-2004, 07:00 PM   #1 (permalink)
    The stug
     
    Shad's Avatar
     
    Join Date: Jul 2003
    Location: In front
    Posts: 2,601
    Thanks: 11
    Thanked 14 Times in 12 Posts
    Shad's system
    Smart 404 handling with ASP.NET

    Moving my image gallery from one domain to another posed a few problems. How would I avoid leaving a trail of red X's behind? What would happen when people follow links to images? Well, I came up with a solution that might be useful to some of you guys...

    Code:
    <%@ Page Language="VB" %>
    <%@Import Namespace="System.Net" %>
    <%@Import Namespace="System.Drawing" %>
    <script language="vb" runat="server">
    	Function Debug(byval thestring as string)
    		Response.Write("<!-- " & thestring & " -->" & vbcrlf)
    	End function
    	
    	Function GetFileContents(FileName As String)
    		Dim objFile As System.IO.File
    		Dim objStreamReader As System.IO.StreamReader
    		objStreamReader = objFile.OpenText(Server.MapPath(FileName))
    		GetFileContents = objStreamReader.ReadToEnd()
    		objStreamReader.Close()
    	End Function
    </script>
    <%
    Dim qs, extns, olddomain, newdomain, galleryroot as string
    
    qs = Request.ServerVariables("QUERY_STRING")
    extns = "jpg jpeg gif png bmp tiff tga"
    olddomain = "http://images.shad.tv"
    newdomain = "http://alfa147.net"
    galleryroot = "/g/"
    
    If instr(lcase(qs),"/stvg/?p=")>0 then
    	'this is an stvg 404, redirect to new path
    	Response.Redirect(newdomain & galleryroot & "?p=" & right(qs,len(qs)-instrrev(qs,"?p=")-2))
    ElseIf right(qs,6) = "/stvg/" or right(qs,5) = "/stvg" then
    	Response.Redirect(newdomain & galleryroot)
    Else
    	dim extn as string = right(qs,len(qs)-instrrev(qs,"."))
    	If instr(extns,extn)>0 then
    		'try to load image from new server, if not found display 404 image
    		Dim webC As New WebClient()
    		Dim strFilename as string
    		strFilename = newdomain & Replace(right(qs,len(qs)-instrrev(qs,olddomain & "/")-(len(olddomain)-1)),"/stvg/",galleryroot)
    		Try
    			' create a bitmap based on the image from the URL 
    			Dim g = New Bitmap(webC.OpenRead(strFilename))
    			If g.RawFormat.Equals(Imaging.Imageformat.Gif) then  
    				Response.ContentType = "image/gif"  
    			Else  
    				Response.ContentType = "image/jpeg"  
    			End if  
    			' send the image to the viewer
    			g.Save(Response.OutputStream, g.RawFormat) 
    			g.Dispose()
    			g = Nothing
    		Catch x as Exception
    			Dim image404 as System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(".") & "\image404.gif")
    			Dim imgFormat = image404.RawFormat
    			Response.ContentType = "image/gif"
    			image404.Save(Response.OutputStream,imgFormat)
    			image404.Dispose()
    			image404 = Nothing
    			imgFormat = Nothing
    		End try
    		webC = Nothing		
    	Else
    		'display 404 page
    		Response.Write(GetFileContents("404b.htm"))
    	End if
    End if
    %>
    
    Basically it will attempt to forward any gallery page requests to the new server. That was a simple thing to do. However if the request is directly for an image file, then it trys to load the image file over the web from the new server and spit it back to the browser. For example, my sig image no longer exists on the domain images.shad.tv, but the script is grabbing it from the new server. If it can't find the image on the new server then it just displays a '404 image' instead. Lastly it none of the above works, it just displays a stock IIS6 404 page.

    Anyway, quite a handy thing for me, hopefully some of it will be of use to you guys

    My name is Simon

    Shad is offline   Reply With Quote
    Reply

    Breadcrumb
    Go Back   HEXUS.community discussion forums > HEXUS.help - buying advice & technical queries > Operating systems & applications > Software and web development


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Trackbacks are On
    Pingbacks are On
    Refbacks are On


    Similar Threads
    Thread Thread Starter Forum Replies Last Post
    ASP.Net hosting Mart Software and web development 0 06-10-2003 08:30 PM
    404 Error page is a loop Rys HEXUS.suggestions 6 05-09-2003 11:34 AM



    All times are GMT. The time now is 04:56 AM.

    Any representations/statements made on the HEXUS.community discussion forums are the representations/statements of the author i.e. the person/organisation making them. If any such representations/statements are disputed they are a matter between the parties concerned.
    HEXUS Limited accepts no responsibility for any misrepresentations, inaccurate or false statements made by any person/organisation other than HEXUS Limited employees.
    For more information please read HEXUS Limited's terms, conditions and privacy policy.

    Hosted Exchange

    Powered by vBulletin® Version 3.8.4
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Content Relevant URLs by vBSEO 3.3.2
    © Copyright 2009 HEXUS® Limited. All rights reserved. Unauthorised reproduction strictly prohibited.