Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't call Response.Redirect inside a static method

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);
}
like image 999
Dvirski Avatar asked Jun 12 '13 19:06

Dvirski


2 Answers

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;
like image 105
Grant Thomas Avatar answered Sep 25 '22 09:09

Grant Thomas


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);
}
like image 28
Karl Anderson Avatar answered Sep 25 '22 09:09

Karl Anderson