Response.Redirect() to redirect to a page in a subfolder

Posted on

Problem :

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.


The URL this tries to redirect to this:


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();

and one more thing both the default page and login page use the same master page.

Solution :

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, …


will give

"<app root>/Account/Login.aspx"

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.

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.

What you need is this:


This will go to Account that reside inside the root and in there to Login.aspx page.

try this


OP says

“Since I moved my login.aspx to my Account 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 there

<location path="Account/login.aspx">
      <allow users="*" />

Once above ^^ is set, this Response.Redirect("~/Account/login.aspx"); certainly works

Leave a Reply

Your email address will not be published. Required fields are marked *