ihttpmodule URL rewrite

R

roko

I build a URL rewrite where a user can go from website.com/345 to go to
website.com?id=345 without seeing the "?id="... pretty neat.

Now, I'm stuck to the point where if I go from http://localhost/website, it
failed, but it's ok if I add another slash, i.e. http://localhost/website/
or http://localhost/website/default.aspx.

Below is the code I used. I would expect that "http://localhost/website"
would go to the default.aspx page as it was...

TIA,
-roko

namespace website.control
{

using System;

using System.Web;

using System.Globalization;

using System.Threading;

using System.IO;

public class MyModule : IHttpModule

{

public void Init(HttpApplication application)

{

application.BeginRequest += (new
EventHandler(this.Application_BeginRequest));

}

private void Application_BeginRequest(Object source, EventArgs e)

{

// Get abbreviated reference to current context

HttpContext Context = HttpContext.Current;

//url string

string requestPath = HttpContext.Current.Request.RawUrl;

if (requestPath.ToLower().ToString() != "/website")

{

string pageName = GetPageName(requestPath);

if(IsNumeric(pageName))

{

Context.RewritePath("story.aspx?id=" + pageName);

}

else

{

switch(pageName.ToLower())

{

case "email":

case "about":

case "advertise":

//case "default":

case "dataentry":

case "event":

case "jobopp":

case "login":

case "onlineform":

case "privacy":

case "register":

case "rss":

case "searchresults":

case "signin":

case "story":

case "user":

Context.RewritePath(pageName + ".aspx");

break;



case "":

Context.RewritePath("default.aspx");

break;

}

}

}

}

// get file name from url requested

public static string GetPageName(string requestPath)

{

if ( requestPath.IndexOf( '?' ) != -1 )

requestPath = requestPath.Substring( 0, requestPath.IndexOf( '?' ) );

return requestPath.Remove( 0, requestPath.LastIndexOf( "/" ) + 1);

}


// IsNumeric Method

// Since C# doesn't have an IsNumeric function, we have to use our own.

public static bool IsNumeric(string strInteger)

{

try

{

int intTemp = Int32.Parse( strInteger );

return true;

}

catch (FormatException)

{

return false;

}

}

// Dispose Method

// This method is required by the HttpModule Interface.

public void Dispose() {}


}

}
 
J

John Saunders

roko said:
I build a URL rewrite where a user can go from website.com/345 to go to
website.com?id=345 without seeing the "?id="... pretty neat.

Now, I'm stuck to the point where if I go from http://localhost/website, it
failed, but it's ok if I add another slash, i.e. http://localhost/website/
or http://localhost/website/default.aspx.

Below is the code I used. I would expect that "http://localhost/website"
would go to the default.aspx page as it was...


Is your code actually being called for http://localhost/website?
 
R

roko

Something to add:

Could it be something to do with aspnet_isapi.dll having extension .* ??

How would I redirect localhost/website or localhost/website/ to default.aspx
or any other default page when it goes thru my customized IHttpModule?

-roko
 
J

John Saunders

roko said:
No it isn't...

If I type manually, "http://localhost/website" -- it is not working
anymore -- returns 404 error. If I add an extra slash at the end, i.e.,
"http://localhost/website/" it works fine. Weird.

So, if your code isn't getting called, then this problem has nothing at all
to do with your code.

In fact, I expect that the problem is with your URL.
"http://localhost/website" is a request for a resource called "website" on
the host called localhost, in the folder "/". "http://localhost/website/"
is a request for a resource called "" on the host called localhost, in the
folder "/website". IIS then uses its list of default documents to find one
which exists in /website, probably finds "default.aspx", then passes that
request off to ASP.NET (and to your code). IIS sees that there is no
resource called "website", so it immediately returns a 404 error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top