Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Response.Redirect with jQuery Mobile - Url hashing

I have a standard forms auth ASP.NET application. My Registration and Login page are in the same .aspx file with 2 jQuery Mobile pages. If I postback my ASP.NET page, such as the user fails to login correctly...etc The Url hash starts appending to itself over and over.

Example Url:
http://localhost:56644/Register.aspx?ReturnUrl=%2fDefault.aspx%3fbla%3dtest&bla=test#Register.aspx?ReturnUrl=%2fDefault.aspx%3fbla%3dtest&bla=test

Once my user is authenticated I want to Redirect to the ReturnUrl without all the hash information or find a way for the url to remain during postbacks?

Markup:

<div data-role="page" id="register">
    <div data-role="content" data-scroll="true" data-theme="b" class="Content">
        ......  
        <a href='#login'>Login</a               
    </div>
</div>
<div data-role="page" id="login">
    <div data-role="content" data-scroll="true" data-theme="b" class="Content">
        .....                             
        <a href='#register' >Registered Yet?</a>
    </div>
</div>

Code-behind on Register.aspx:

protected void btnLogin_Click(object sender, EventArgs e)
{        
    if (LoggedIn)
    {
        FormsAuthentication.SetAuthCookie("blabla", true); 
        //Note: Request.QueryString["ReturnUrl"] = "/Default.aspx?bla=test";
        Response.Redirect(Request.QueryString["ReturnUrl"]);

    }
}
like image 567
rick schott Avatar asked Jul 28 '11 18:07

rick schott


People also ask

How to manage a redirect request after a jQuery call?

You can manage a redirect request after a jQuery call by using an approach by JSON. All the responses to Ajax requests have the status code 200 and the body of the response containing a JSON object that is constructed on the server.

How do Ajax redirects work in JavaScript?

All the responses to Ajax requests have the status code 200 and the body of the response containing a JSON object that is constructed on the server. On the client, JavaScript can use the JSON object to decide further actions. The browser processes the redirect and delivers a 200 code with the content of the redirect destination.

What is redirecttoaction method in ASP NET Core?

The RedirectToAction method is used to redirect a request in ASP.NET Core from one URL to another. This can be used to redirect based on some condition. The method is part of the Controllerbase class so it’s directly available for use in the controller class.

What is the status code for Ajax request?

All the responses to Ajax requests have the status code 200 and the body of the response containing a JSON object that is constructed on the server. On the client, JavaScript can use the JSON object to decide further actions.


1 Answers

This is an old post but having experienced the same issue I'll post the solution I have worked out - it is a bit rough but it may help someone or be improved. Moreover it is in ASP.NET MVC 4 - not sure how to migrate the same code to aspx

What I am basically doing is capturing the RedirectTo URL and using it to provide as data-url attribute of the LogOn form tag. In other terms, in MVC 4:

  1. I create a copy of the LogOn.csthml as LogOn.Mobile.cshtml
  2. in LogOn.Mobile.cshtml I add the following: :

     @{
     string landPage = Request.Url.Query.Length>11?
    Request.Url.Query.Substring(11):"";//very rough, to be improved. 
       // Here I am clipping the RedirectTo prefix of the Query
     }
    //replaces the boilerplate @using (Html.BeginForm())
    @using (Html.BeginForm("LogOn", "Account", FormMethod.Post, 
      new { @data_url = landPage})) 
    

This should enough to make it work

like image 180
eddo Avatar answered Oct 05 '22 23:10

eddo