troubleshooting Question

Trying to 404 Redirect to mimic an internal TinyUrl using ASP and / or C#

Avatar of christianl
christianl asked on
ASP.NET ProgrammingC#
1 Comment1 Solution722 ViewsLast Modified:
Objective: To mimic something of that of tinyurl but internally hosted, but not have that random text at the end of the url but be something that you can specify inside of a database.  

Environment: OS = Windows 2003 , Service = IIS 6.0

Status: Currently have a working prototype but trying to make it work in a different way. Currently how it works is if the user goes to http://example.com/k.aspx?k=Books , it then sees that the value of "k" is set to "Books" then connects to the MSSQL (KLinks) database and searches for an entry for "Books". If an entry is found for "Books" it grabs what the long url should be for that reference "TrueUrl".  This "TrueUrl" for "Books" may be an entry like http://example.com/library/reference/clients/teachers/start/notes/books.html , once it has grabbed this value from the database it will redirect (i don't care about rewrite) the user to the link http://example.com/library/reference/clients/teachers/start/notes/books.html. So long story short when a user goes to http://example.com/k.aspx?k=Books it connects to a database, looks up an entry for Books and redirects to http://example.com/library/reference/clients/teachers/start/notes/books.html.

Where I'm stuck: So currently to use the script you have to use a link like http://example.com/k.aspx?k=Books ...but  I want to be able to use it like this http://example.com/Books .

So I've read to solve this i need to use the script as part of my 404 error and parse out the last part of of the query string to pull out the "Books" in a link like http://example.com/Books . So the original script I've tested and works when using the link http://example.com/k.aspx?k=Books I've set to be the 404 error page for that site.

Since the original script requires you to set the value of "k" to work, the script no longer works when it's set to the 404 error page.

I'm not very well versed in C# so a bit stuck on how to edit the existing script (Pasted in the Code Snippet) to be the 404.asp and parse out the the requested string and grab the last part of the link after the last "/".

A good example in asp that i found that is very similar to what I'm trying to do is below. (I can't seem to mimic this in C# in the script (in the code snippet)

----asp example start----
SomeDir = Request.ServerVariables("Query_String")
SomeDir = UCase(Mid(SomeDir, InStrRev(SomeDir, "/") + 1, Len(SomeDir)))
'SomeDir now contains whatever the user typed after the last
'slash in the URL, if he typed http://www.foo.com/books SomeDir
'will contain "BOOKS".
----asp example end----

Conclusion: Anyone able to show me how to edit the code in the snippet to work as a 404 error to redirect like the script i originally attached. I'm looking to no longer use http://example.com/k.aspx?k=Books but instead have something like http://example.com/Books

-Thanks
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //URL used to enter:
        //http://k.kmaone.com/k.aspx?k=kma
        String Kid;
        String URL;
        String KValue = Request.QueryString["k"].ToLower();
        
        
                
        //Open K database and pull
        //  all Active records
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("Data Source=MSSQL;Initial Catalog=K;Persist Security Info=True;User ID=IDGoesHere;Password=PasswordGoesHere");
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT TrueURL, KID FROM KLinks WHERE Active = 1", conn);
        conn.Open();
        System.Data.SqlClient.SqlDataReader myReader = cmd.ExecuteReader();
 
        //loop over all Active Records
        while (myReader.Read())
            {
                Kid = myReader.GetString(1).Trim().ToLower();
                URL = myReader.GetString(0).Trim().ToLower();
 
                //if database key matches URL Parameter...
                if (KValue == Kid)
                {
                    //redirect to URL
                    Response.Redirect(URL);
                    Response.End();
                }
                
 
            }
        myReader.Close();
        conn.Close();
 
 
    }
   
  
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>KFiles</title>
</head>
<body>
    The link you entered is invalid.&nbsp; Please check the source and try again.
 
</body>
</html>
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros