I recommend you use Redirect from the Application_BeginRequest event
instead. If you do not want to show the new url to the user, then use
URL rewriting.
http://www.aspnetpro.com/NewsletterA...200309pj_l.asp
I also recommend you don't hardcode "http". What if it is accessed via
https? Instead use HttpContext.Current.Request.Url.Scheme. Below is a
method that I used in a similar situation. This is a directory
application, where the subweb was in a sub-folder of the main
application. I used an app setting to define the "authority" used to
access the sub-web. You may need to alter this slightly where I look
for and add 'PhoneDirectory' to the url path.
web.config (dev server):
<add key="Phone.HostAuthority" value="DevelopmentServerName:150/" />
web.config (prod server):
<add key="Phone.HostAuthority" value="www.MyWebDomain.com" />
don't forget to turn on tracing to see the trace statements appear on
the page (during testing):
<trace enabled="true" requestLimit="10" pageOutput="true"
traceMode="SortByTime" localOnly="true" />
global.asax.cs:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string phoneAuthority =
ConfigurationSettings.AppSettings["Phone.HostAuthority"];
HttpRequest req = HttpContext.Current.Request;
TraceContext trace = HttpContext.Current.Trace;
if (trace.IsEnabled)
{
trace.Write("MyApp", "Calculating Path - used to determine what Url
variables are helpful");
trace.Write("MyApp", "ApplicationPath=" + req.ApplicationPath);
trace.Write("MyApp", "RawUrl=" + req.RawUrl);
trace.Write("MyApp", "AbsolutePath="+req.Url.AbsolutePath);
trace.Write("MyApp", "AbsoluteUri="+req.Url.AbsoluteUri);
trace.Write("MyApp", "Authority="+req.Url.Authority);
trace.Write("MyApp", "Fragment="+req.Url.Fragment);
trace.Write("MyApp", "Host="+req.Url.Host);
trace.Write("MyApp",
"HostNameType="+req.Url.HostNameType.ToString( ));
trace.Write("MyApp",
"IsDefaultPort="+req.Url.IsDefaultPort.ToString()) ;
trace.Write("MyApp", "Port="+req.Url.Port.ToString());
trace.Write("MyApp",
"AppSettings:Phone.HostAuthority="+phoneAuthority) ;
}
if (phoneAuthority == null || phoneAuthority.Length == 0)
return;
string requestAuthority = req.Url.Authority + req.ApplicationPath;
if (requestAuthority == phoneAuthority)
{ //authority matches, let's ensure 'PhoneDirectory' is part of path.
string appPath = req.ApplicationPath;
if (!appPath.EndsWith("/"))
appPath += "/";
string requestedPath =
req.Url.AbsolutePath.Substring(appPath.Length);
if (!requestedPath.ToLower().StartsWith("phonedirecto ry/"))
{
string newUrl = req.Url.Scheme + "://" + req.Url.Authority;
newUrl += appPath + "PhoneDirectory/" + requestedPath;
if (trace.IsEnabled)
trace.Write("MyApp", "Redirecting to:" + newUrl);
HttpContext.Current.Response.Redirect(newUrl);
}
else
{
if (trace.IsEnabled)
trace.Write("MyApp", "Url already includes '/PhoneDirectory/'");
}
}
else
{
if (trace.IsEnabled)
trace.Write("MyApp", "Phone.HostAuthority does not match current
request authority. No url action required.");
}
}