can't call Response.Redirect inside a static method

27,636

Solution 1

You will need to return the constructed URL to the client:

public static string redirect_to_profile()
{
    dbservices db=new dbservices();
    string user = HttpContext.Current.User.Identity.Name;
    string id = db.return_id_by_user(user);
    return "personal_profile.aspx?id="+id;
}

Then using JavaScript, in the success function of the AJAX call, set the location:

window.location = res;

Or perhaps:

window.location = res.d;

Solution 2

You need to have your web method pass back the ID of the user whose profile you want to redirect to, then in your jQuery success callback set the window.location to the path plus the query string, like this:

function redirect_to_profile() {
    $.ajax({
        type: "POST",
        url: "personal_profile.aspx.cs.aspx/redirect_to_profile",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (res) {
            // Redirect to personal_profile.aspx passing it the ID we got back from the web method call
            window.location = "personal_profile.aspx?id=" + res;
        },
        error: function (res, msg, code) {
            // log the error to the console
        } //error
    });
}

[WebMethod]
public static string redirect_to_profile()
{
    dbservices db=new dbservices();
    string user = HttpContext.Current.User.Identity.Name;
    return db.return_id_by_user(user);
}

Solution 3

Instead of doing HttpResponse.Redirect you can send this url that you have generated to you Javascript (response to the ajax call) and then use Javascript code to redirect.

Share:
27,636
Dvirski
Author by

Dvirski

Updated on December 20, 2020

Comments

  • Dvirski
    Dvirski over 3 years

    Hello I'm trying to run a webmethod with ajax from an aspx page. basically I want to redirect to another aspx page with a query string, but I want to do it from <a href>, beacuse it's part of a jquery menu.

    from what I learned I can only use ajax to call static webmethods, but I canot redirect from my static function.

    visual studio marks it in a red line saying: "an object reference is required for the nonstatic field method or property System.Web.HttpResponse.Redirect(string)"

    here is the ajax call:

    function redirect_to_profile() {
        $.ajax({
            type: "POST",
            url: "personal_profile.aspx.cs.aspx/redirect_to_profile",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (res) {
               alert("success");
            },
            error: function (res, msg, code) {
                // log the error to the console
            } //error
        });
    }
    

    here is the a href:

    <a  onclick="redirect_to_profile()">Personal Profile</a>
    

    here is the webmethod inside the personal_profile.aspx

    [WebMethod]
    public static void redirect_to_profile()
    {
    
        dbservices db=new dbservices();
        string user = HttpContext.Current.User.Identity.Name;
        string id = db.return_id_by_user(user);
    
        HttpResponse.Redirect("personal_profile.aspx?id="+id);
    }
    
    • SLaks
      SLaks almost 11 years
      1) There is no static Response. 2) That won't do you want anyway. You need to talk to the JS.
    • Dvirski
      Dvirski almost 11 years
      i tried to talk to it, it won't answer : ) what do you mean by that?
    • SLaks
      SLaks almost 11 years
      You need to return a result that tells the JS what to do.
    • gilly3
      gilly3 almost 11 years
      Why are you using AJAX? It sounds like you want a link click to result in the browser loading a different page. AJAX is a poor choice if you want to just load a new page.
  • Ian Campbell
    Ian Campbell almost 11 years
    Thanks for this @Grant! What does the .d indicate? The response itself is just an object, whereas the .d seems to get the string value...
  • QMaster
    QMaster over 9 years
    Just the code without any description is not useful enough. Users couldn't find relation between question and answer. Additionally I think this answer completely unrelated to the question.
  • IMRUP
    IMRUP almost 7 years
    res.d is false.. not string