Response.Redirect() to redirect to a page in a subfolder
Asked Answered
M

6

6

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.

Matutinal answered 18/12, 2011 at 11:55 Comment(7)
-1 for not even trying to post readable question.Lemmy
At least paste code without spelling errors (English errors are acceptable, but what you posted has typoes on response everywhere and the URL you post and the redirect line don't match up)Amphibiotic
edited .. i need to go from root folder to subfolder name Accont and to a page named login.aspx .. how toMatutinal
@ShadowWizard now read it ...Matutinal
Still full with errors, including your code.Lemmy
That's got to be one of the fuller edits I've ever done!Desulphurize
@Jon good job - user614946, please see what Jon did and do the same in the future. Posting full code is not what anyone here asked and really not relevant.Lemmy
G
6

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 :

/BuzzEnhance/Account/Account/Login.aspx

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

Gilgai answered 18/12, 2011 at 12:38 Comment(0)
S
5
"~/Account/Login.aspx"

will give

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

so if your apps' root is

http://localhost/BuzzEnhance

the relative path given will be expanded to

http://localhost/BuzzEnhance/Account/Login.aspx

Also if you're using Forms Authentication, you may want to use

FormsAuthentication.RedirectToLoginPage();

see MSDN.

Shurlocke answered 18/12, 2011 at 12:15 Comment(12)
@ShadowWizard it still HTTP Error 400 - Bad Request.Matutinal
@user614946: Note that I've removed port number as being unimportant.Shurlocke
@user614946: Does url http://<any>/BuzzEnhance/Account/Login.aspx really exist?Shurlocke
yes the folder is there and the file Login.aspx is in it ... i cant figure it out that why i posted it in here ..@ShadowWizardMatutinal
@user614946: Account/Account/Login.aspx or just Account/Login.aspx ?!Shurlocke
just Account/Login.aspx @ShurlockeMatutinal
@user614946: then "~/Account/Login.aspx" should work. Try use Server.MapPath(string) to check how is ~ being expended.Shurlocke
i dont know why its not working .. i am using my above given code .. and "~/Account/Login.aspx but i get problem loading page!Matutinal
@user614946: Have you tried Server.MapPath() to debug resulting path?Shurlocke
@Shurlocke yes and it gives me the path but of my machine .. when i give our method a try it say problem loading page.. and no auths ..Matutinal
@Matutinal - "...it say problem loading page." probably because Login.aspx is bound to the Master page, and it's looping - see my answer.Gilgai
@user614946: Yes, it's so it works: converts relative web path to physical, so you could check does it really exist, etc. Does it?Shurlocke
T
2

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.

Therewithal answered 18/12, 2011 at 12:2 Comment(4)
Problem is the page with the code is already inside different sub folder, called "BuzzEnhance" so the relative path is relative to that location.Lemmy
@ShadowWizard so now what should i do??Matutinal
@user614946: Post the whole app folder structure, please.Shurlocke
BuzzEnhance\Account\Login.aspx .. i tested when i put it in the root and call only response.redirect("login.aspx"); page is called .. but when placed in Account folder which is in the root folder it gives error @ShurlockeMatutinal
L
1

What you need is this:

Response.Redirect("/Account/Login.aspx");

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

Lemmy answered 18/12, 2011 at 12:10 Comment(1)
no luck with that it gives me localhost:10773/Account/Login.aspx but what i need is localhost:10773/BuzzEnhance/Account/Login.aspxMatutinal
A
0

try this

Response.Redirect(this.ResolveClientUrl("~/Account/Login.aspx");
Alterable answered 6/10, 2015 at 15:31 Comment(0)
J
0

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">
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>

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

Jijib answered 1/4, 2021 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.