2012-10-11 41 views
2

我在我的webapp上有2個頁面。 Login.aspx和Main.aspx。Asp.net重定向從登錄頁面到主頁面

成功登錄用戶名和密碼後,我從login.aspx重定向到Main.aspx,如下面的c#中所示。 這在visual studio 2010中工作正常。 問題是,當我部署我的網站時,localhost的值沒有意義。

我可以確定網站正在運行的服務器名稱,或者我應該將服務器重定向主頁面鏈接放在我的web.config文件中?

感謝 達摩

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

回答

9

如何

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

這是執行此操作的「默認」方式。

+0

好的 - 我將使用這 – user1438082

+0

這是問題的正確答案,但我不會在代碼中硬編碼我的登錄重定向頁面。相反,我會設置RedirectURL = Page.ResolveUrl(「〜/」);然後在web.config文件中爲站點設置默認文檔。 (這是假設網站具有所需的身份驗證,並且如果用戶未通過身份驗證,則將用戶重定向到登錄頁面)。 –

1

可以使用服務器變量SERVER_NAME

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

如果有很多網站怎麼辦? – Aristos

+0

這將獲取在請求的URL中指定的服務器的名稱。因此'http:// server1.com/something.aspx'會在代碼運行後被重定向到'http:// server1.com/Main.aspx'。 –

+0

嗨,回答正確 - 你可以添加「:」這裏給你的答案,然後我會標記你的正確。字符串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」); – user1438082

1

我的建議是把服務器名在web.config文件和下Application_Start事件的Global.asax文件中加載

web.config文件中:

<appSettings> 
    <add key="Domain" value="yourdomain" /> 
</appSettings> 
Global.asax文件

protected void Application_Start(object sender, EventArgs e) 
{ 
    try 
    { 
     SomeStaticGlobalClass.Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; 
    } 
    catch { } 
} 
+0

這是另一件需要配置(可能忘記配置)的事情,可以從請求的URL中取得。 –

+0

如果你要將域名更改爲子域名或其他內容,該怎麼辦?不要忘記,在IIS中,你可以配置多個域到應用程序 – udidu