Response.Redirect() 重定向到子文件夹中的页面
我正在使用 Response.Redirect("login.aspx");
自从我将 login.aspx
移动到我的 Account
子文件夹后,我尝试了下面的代码,但是它不起作用。
Response.Redirect("Account/login.aspx");
尝试重定向到的 URL:
http://localhost/BuzzEnhance/Account/Login.aspx
完整代码是:
public partial class BuzzMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Username"] != null)
{
username.Text = Session["Username"].ToString();
}
else
{
Response.Redirect("Account/Login.aspx");
}
}
}
}
还有一件事是默认页面和登录页面使用相同的母版页。
I am using a Response.Redirect("login.aspx");
Since I moved my login.aspx
to my Account
subfolder, I tried the following code, however it doesn't work.
Response.Redirect("Account/login.aspx");
The URL this tries to redirect to this:
http://localhost/BuzzEnhance/Account/Login.aspx
The full code is:
public partial class BuzzMaster : MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Username"] != null)
{
username.Text = Session["Username"].ToString();
}
else
{
Response.Redirect("Account/Login.aspx");
}
}
}
}
and one more thing both the default page and login page use the same master page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的问题是您正在从 MasterPage 进行重定向并使用相对路径。
当您使用相对路径时,它将相对于绑定到母版页的内容页的位置,而不是相对于母版页的位置。
您重定向到 :
几乎肯定来自绑定到您的母版页的帐户文件夹中的内容页面。例如,如果您的登录页面 (/BuzzEnhance/Account/Login.aspx) 本身绑定到该母版页,它将重定向到相对路径
Account/Login.aspx
,该路径将解析为 < code>/BuzzEnhance/Account/Account/Login.aspx,正是您所看到的。最好的解决方案是 @abatishchev 的答案 - 使用相对于应用程序根
~/Account/Login.aspx
的路径。但是,如果正如我怀疑的那样,您的 Login.aspx 页面绑定到同一个母版页,这会给您带来另一个问题。每次访问Login.aspx时,它都会执行母版页中的重定向代码,从而导致无限循环,直到超时。
一种解决方案是避免将 Login.aspx 页面绑定到该母版页,或者添加一些条件代码,以便您在 Login.aspx 页面上时不会重定向。
尽管更好,但如果您使用表单身份验证并让它以标准方式管理登录页面的重定向,则根本不需要执行重定向。如果要显示用户名,可以使用
HttpContext.Current.User.Identity.Name
- 或使用 ASP.NET 登录控件之一:LoginStatus
、登录名
, ...Your problem is that you're doing a redirect from a MasterPage, and using a relative path.
When you use a relative path, it will be relative to the location of the content page that is bound to the master page, not relative to the location of the Master Page.
Your redirection to :
is almost certainly coming from a content page in the Account folder that is bound to your master page. For example, if your Login page (/BuzzEnhance/Account/Login.aspx) is itself bound to that Master page, it will redirect to the relative path
Account/Login.aspx
, which will resolve to/BuzzEnhance/Account/Account/Login.aspx
, exactly what you're seeing.The best solution is in the answer from @abatishchev - use a path relative to the application root
~/Account/Login.aspx
.However, this will give you another problem if, as I suspect, your Login.aspx page is bound to the same master page. Each time you access Login.aspx, it will execute the redirect code in the master page, resulting in an infinite loop until something times out.
One solution is either to avoid binding your Login.aspx page to that Master page, or to add some conditional code so you don't redirect when on the Login.aspx page.
Though even better, you should not need to do a redirect at all if you use Forms Authentication and leave it to manage redirection to the login page in the standard way. If you want to display the username, you can use
HttpContext.Current.User.Identity.Name
- or use one of the ASP.NET Login controls:LoginStatus
,LoginName
, ...将给出
,如果您的应用程序的根目录是
给定的相对路径,则将扩展为
此外,如果您使用表单身份验证,您可能需要使用
请参阅 MSDN。
will give
so if your apps' root is
the relative path given will be expanded to
Also if you're using Forms Authentication, you may want to use
see MSDN.
首先,它不是
responce.redirect("page.aspx");
它的
Response.Redirect("Page.aspx");
尝试以这种方式它将起作用。根据您的问题,其
Response.Redirect("folder/page.aspx");
现在尝试,我会等待。请记住,在 C# 中第一个字母应大写。
First of all its not
responce.redirect("page.aspx");
its
Response.Redirect("Page.aspx");
Try in this way it will work. As per your question its
Response.Redirect("folder/page.aspx");
try now, I will be waiting.Keep in mind in C# first letter should be capitalized.
您需要的是:
这将转到驻留在根目录中的
Account
,并在其中转到Login.aspx
页面。What you need is this:
This will go to
Account
that reside inside the root and in there toLogin.aspx
page.试试这个
try this
OP 说
“自从我将
login.aspx
移至我的Account
子文件夹后,我尝试了以下代码,但它不起作用。”还有另一种情况可能会导致某些行为。
在
web.config
中,您可能有允许的位置。除非您打开一个已添加的此类位置,否则您将不会被重定向到那里。一旦设置了上述 ^^,这个 Response.Redirect("~/Account/login.aspx"); 肯定会 被重定向到那里作品
OP says
"Since I moved my
login.aspx
to myAccount
subfolder, I tried the following code, however it doesn't work."There is another case that might cause exactly some behavior.
In the
web.config
you might have permitted locations. And unless you open one such location that you've added, you will not be redirected thereOnce above ^^ is set, this
Response.Redirect("~/Account/login.aspx");
certainly works