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() {}
}
}
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() {}
}
}