2

i have 2 pages on my webapp. Login.aspx and Main.aspx.

After a successful login username and password i redirect from login.aspx to Main.aspx as shown below in c#. This works fine in visual studio 2010. The problem is that when i deploy my website the value localhost does not make sense.

Can i determine the server name that the website is running on or should i somehow place the server redirect main page link in my web.config file?

thanks Damo

string Redirectport = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
RedirectURL = "http://localhost:" + Redirectport + System.Web.HttpContext.Current.Response.ApplyAppPathModifier("~/Main.aspx");
0

4 Answers 4

9

How about

RedirectURL = Page.ResolveUrl("~/Main.aspx") ?

this is the "default" way to do it.

1
  • This is correct answer for the question, But I would not hard code my login redirect page in code. Instead I will set RedirectURL = Page.ResolveUrl("~/"); And then set default document for the site in web.config file. (This is assuming site has authentication required and redirects user to Login page if user is not authenticated).
    – Raghu A
    Jun 5, 2015 at 16:03
1

My suggestion is to put the server name in the web.config file and load it in the Global.asax file under Application_Start event

in the web.config file:

<appSettings>
    <add key="Domain" value="yourdomain" />
</appSettings>

in Global.asax file:

protected void Application_Start(object sender, EventArgs e)
{
    try
    {
        SomeStaticGlobalClass.Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"];
    }
    catch { }
}
2
  • That's one more thing to configure (and maybe forget to configure) that can instead be taken from the requested URL.
    – Eric J.
    Oct 11, 2012 at 20:45
  • And what if you gonna change your domain to a subdomain or anything else? dont forget that in IIS, you can configure multiple domains to application
    – udidu
    Oct 11, 2012 at 20:47
1

You can use the server variable SERVER_NAME

string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"]
RedirectURL = "http://" + serverName + ":" + Redirectport + 
    System.Web.HttpContext.Current.Response.ApplyAppPathModifier("~/Main.aspx");
5
  • That will get the name of the server specified in the requested URL. So http://server1.com/something.aspx would be redirected to http://server1.com/Main.aspx after that code runs.
    – Eric J.
    Oct 11, 2012 at 20:44
  • hi, answer correct - can you add the ":" as shown here to your answer then i will mark yours as correct. string Redirectport = HttpContext.Current.Request.ServerVariables["SERVER_PORT"]; string serverName = HttpContext.Current.Request.ServerVariables["SERVER_NAME"]; RedirectURL = "http://" + serverName + ":" + Redirectport + System.Web.HttpContext.Current.Response.ApplyAppPathModifier("~/Main.aspx"); Oct 11, 2012 at 21:00
  • This answer works for the specific situation of the OP, but in most cases the URL won't point to the server's root. Instead it will be something like http://server1.com/SiteA/Main.aspx
    – daniloquio
    Oct 11, 2012 at 21:07
  • thank you eric and danoloquio thanks for the heads up on that potential issue. regards damo Oct 11, 2012 at 21:23
  • @daniloquio. Agree. I upvoted the accepted answer, but am leaving mine because it may be situationally valuable to someone.
    – Eric J.
    Oct 11, 2012 at 22:22
0

if you want navigate to some internal web page , you can use something like below

Response.Redirect("~/Default.aspx");

you also can use it navigate to other server

Response.Redirect("https://www.google.com/");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.