Server.Transfer

M

Mark Sandfox

When i use Server.Transfer to transfer a URL (IWillBeHeard.com) to a
directory within the Home Website (Different URL) it still references links
and images to IWillBeHeard.com/images/graphic.jpg instead of
images/grapic.jpg. The code i am using to transfer is below. I must be
missing something. I have tried this with a SubWeb and a simply directory
and get the same results both ways. The ideal way is using SubWebs.

Thank you in advance for your help.

<Script Runat="server">
dim strToString, strLocalPath, strPathAndQuery as string

Sub page_load(Sender as Object, E as EventArgs)
strToString = request.URL.tostring
strLocalPath = request.URL.LocalPath
strPathAndQuery = request.URL.PathAndQuery
strToString = replace(strToString, strPathAndQuery, "")
strToString = replace(strToString, "http://www.", "")
strToString = replace(strToString, "http://", "")

Select Case strToString
Case "youthgroupservices.com"
Server.Transfer("Home.htm")
Case "iwillbeheard.com"
Server.Transfer("IWillBeHeard/Default.htm")
End Select


End Sub
</script>
 
M

Mike

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/NewsletterArticle/2003/09/asp200309pj_l/asp200309pj_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("phonedirectory/"))
{
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.");
}
}
 
B

Bruce Barker

if you use server transfaer, the browser will not know the new url, but used
the request url to calc releative paths.

-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top