I recommend you use Redirect from the Application_Beg inRequest 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.Cur rent.Request.Ur l.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.Host Authority" value="Developm entServerName:1 50/" />
web.config (prod server):
<add key="Phone.Host Authority" value="www.MyWe bDomain.com" />
don't forget to turn on tracing to see the trace statements appear on
the page (during testing):
<trace enabled="true" requestLimit="1 0" pageOutput="tru e"
traceMode="Sort ByTime" localOnly="true " />
global.asax.cs:
protected void Application_Beg inRequest(Objec t sender, EventArgs e)
{
string phoneAuthority =
ConfigurationSe ttings.AppSetti ngs["Phone.HostAuth ority"];
HttpRequest req = HttpContext.Cur rent.Request;
TraceContext trace = HttpContext.Cur rent.Trace;
if (trace.IsEnable d)
{
trace.Write("My App", "Calculatin g Path - used to determine what Url
variables are helpful");
trace.Write("My App", "ApplicationPat h=" + req.Application Path);
trace.Write("My App", "RawUrl=" + req.RawUrl);
trace.Write("My App", "AbsolutePath=" +req.Url.Absolu tePath);
trace.Write("My App", "AbsoluteUri="+ req.Url.Absolut eUri);
trace.Write("My App", "Authority="+re q.Url.Authority );
trace.Write("My App", "Fragment="+req .Url.Fragment);
trace.Write("My App", "Host="+req.Url .Host);
trace.Write("My App",
"HostNameType=" +req.Url.HostNa meType.ToString ());
trace.Write("My App",
"IsDefaultPort= "+req.Url.IsDef aultPort.ToStri ng());
trace.Write("My App", "Port="+req.Url .Port.ToString( ));
trace.Write("My App",
"AppSettings:Ph one.HostAuthori ty="+phoneAutho rity);
}
if (phoneAuthority == null || phoneAuthority. Length == 0)
return;
string requestAuthorit y = req.Url.Authori ty + req.Application Path;
if (requestAuthori ty == phoneAuthority)
{ //authority matches, let's ensure 'PhoneDirectory ' is part of path.
string appPath = req.Application Path;
if (!appPath.EndsW ith("/"))
appPath += "/";
string requestedPath =
req.Url.Absolut ePath.Substring (appPath.Length );
if (!requestedPath .ToLower().Star tsWith("phonedi rectory/"))
{
string newUrl = req.Url.Scheme + "://" + req.Url.Authori ty;
newUrl += appPath + "PhoneDirec tory/" + requestedPath;
if (trace.IsEnable d)
trace.Write("My App", "Redirectin g to:" + newUrl);
HttpContext.Cur rent.Response.R edirect(newUrl) ;
}
else
{
if (trace.IsEnable d)
trace.Write("My App", "Url already includes '/PhoneDirectory/'");
}
}
else
{
if (trace.IsEnable d)
trace.Write("My App", "Phone.HostAuth ority does not match current
request authority. No url action required.");
}
}