Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net从登录重定向到主页面_C#_Asp.net_Web Config_Iis 7.5 - Fatal编程技术网

C# Asp.net从登录重定向到主页面

C# Asp.net从登录重定向到主页面,c#,asp.net,web-config,iis-7.5,C#,Asp.net,Web Config,Iis 7.5,我的webapp上有2个页面。Login.aspx和Main.aspx 成功登录用户名和密码后,我从login.aspx重定向到Main.aspx,如下面的c#所示。 这在VisualStudio2010中运行良好。 问题是,当我部署我的网站时,localhost的值没有意义 我是否可以确定网站运行的服务器名称,或者是否应该在web.config文件中放置服务器重定向主页链接 谢谢 达莫 您可以使用服务器变量server\u NAME string serverName = HttpContex

我的webapp上有2个页面。Login.aspx和Main.aspx

成功登录用户名和密码后,我从login.aspx重定向到Main.aspx,如下面的c#所示。 这在VisualStudio2010中运行良好。 问题是,当我部署我的网站时,localhost的值没有意义

我是否可以确定网站运行的服务器名称,或者是否应该在web.config文件中放置服务器重定向主页链接

谢谢 达莫


您可以使用服务器变量server\u NAME

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

我的建议是将服务器名放在
web.config
文件中,并将其加载到应用程序启动事件下的
Global.asax
文件中

web.config
文件中:

<appSettings>
    <add key="Domain" value="yourdomain" />
</appSettings>
protected void Application_Start(object sender, EventArgs e)
{
    try
    {
        SomeStaticGlobalClass.Domain = System.Configuration.ConfigurationManager.AppSettings["Domain"];
    }
    catch { }
}
怎么样

RedirectURL=Page.ResolveUrl(“~/Main.aspx”)


这是一种“默认”方式。

如果您想导航到某个内部网页,可以使用以下方法

Response.Redirect("~/Default.aspx");
您还可以使用它导航到其他服务器

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

将获取请求的URL中指定的服务器名称。所以
http://server1.com/something.aspx
将被重定向到
http://server1.com/Main.aspx
代码运行后。您好,回答正确-您能否在您的答案中添加“:”如图所示,然后我将您的答案标记为正确。string Redirectport=HttpContext.Current.Request.ServerVariables[“服务器_端口”];字符串serverName=HttpContext.Current.Request.ServerVariables[“服务器名称”];RedirectURL=“http://”+serverName+:“+Redirectport+System.Web.HttpContext.Current.Response.ApplyAppPathModifier(“~/Main.aspx”);这个答案适用于OP的特定情况,但在大多数情况下URL不会指向服务器的根。而是类似于
http://server1.com/SiteA/Main.aspx
谢谢eric和danoloquio感谢大家对这一潜在问题的关注。当做damo@daniloquio. 同意。我对接受的答案投了赞成票,但我离开了我的答案,因为它可能对某些人有实际价值。这是另外一件需要配置的事情(可能忘记配置),可以从请求的URL中获取。如果你要将你的域更改为子域或其他任何内容,该怎么办?不要忘记,在IIS中,您可以将多个域配置为应用程序。这是问题的正确答案,但我不会用代码硬编码我的登录重定向页面。相反,我将设置RedirectURL=Page.ResolveUrl(“~/”);然后在web.config文件中为站点设置默认文档。(这是假设站点需要身份验证,并在用户未经身份验证时将用户重定向到登录页面)。
Response.Redirect("https://www.google.com/");