can't call Response.Redirect inside a static method
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.
Dvirski
Updated on December 20, 2020Comments
-
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 almost 11 years1) There is no static
Response
. 2) That won't do you want anyway. You need to talk to the JS. -
Dvirski almost 11 yearsi tried to talk to it, it won't answer : ) what do you mean by that?
-
SLaks almost 11 yearsYou need to return a result that tells the JS what to do.
-
gilly3 almost 11 yearsWhy 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 almost 11 yearsThanks 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 over 9 yearsJust 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 almost 7 yearsres.d is false.. not string