在静态方法中无法调用Response.Redirect。

10

你好,我正在尝试从一个aspx页面使用ajax运行webmethod。基本上,我想通过<a href>来重定向到另一个带有查询字符串的aspx页面,因为它是jquery菜单的一部分。

据我所学,我只能使用ajax调用静态webmethod,但我不能从我的静态函数重定向。

Visual Studio将其标记为红色线条,并显示以下内容:"an object reference is required for the nonstatic field method or property System.Web.HttpResponse.Redirect(string)"

这里是ajax调用:

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
    });
}

这是一个超链接:

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

这里是 personal_profile.aspx 内的 WebMethod。

[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);
}

1
  1. 没有静态的“Response”。
  2. 那样做也不会让你得到想要的结果。你需要与JS交流。
- SLaks
1
我试图与它交流,但它不回应:)你是什么意思? - Dvirski
1
你需要返回一个结果,告诉JS该做什么。 - SLaks
1
你为什么要使用AJAX呢?听起来你想让链接点击后在浏览器中加载不同的页面。如果你只想加载一个新页面,那么使用AJAX是一个不好的选择。 - gilly3
4个回答

21
您需要将构建的URL返回给客户端:
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;
}

然后使用JavaScript,在AJAX调用的success函数中设置位置:

window.location = res;

或者:

window.location = res.d;

谢谢你,@Grant!.d是什么意思?响应本身只是一个对象,而.d似乎获取了字符串值... - Ian Campbell
res.d 是 false。不是字符串。 - IMRUP

3

你需要确保你的web方法返回要重定向到的用户ID,然后在jQuery成功回调函数中将window.location设置为路径加上查询字符串,像这样:

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);
}

2

不需要使用HttpResponse.Redirect,你可以将生成的URL发送给JavaScript(响应ajax调用),然后使用JavaScript代码进行重定向。


0

试试这个:

function myFun(a) {
            var s = null;
            var em = document.getElementById(a + 'productno');
            if (em != null)
                PageMethods.setSession(em.innerText);
            window.location.assign("/Product%20Details.aspx");


        }

2
仅有代码而没有任何描述是不够有用的。用户无法找到问题和答案之间的关系。此外,我认为这个答案与问题完全无关。 - QMaster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接