C# 不能在静态方法中调用 Response.Redirect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17073362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:30:52  来源:igfitidea点击:

can't call Response.Redirect inside a static method

c#javascriptasp.netajaxstatic-methods

提问by Dvirski

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.

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

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

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

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)"

Visual Studio 用红线标记它说:“非静态字段方法或属性 System.Web.HttpResponse.Redirect(string) 需要一个对象引用”

here is the ajax call:

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

here is the a href:

这是一个href:

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

here is the webmethod inside the personal_profile.aspx

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

采纳答案by Grant Thomas

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

您需要将构造的 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;
}

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

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

window.location = res;

Or perhaps:

也许:

window.location = res.d;

回答by Guanxi

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.

您可以将生成的这个 url 发送给您的 Javascript(对 ajax 调用的响应),然后使用 Javascript 代码进行重定向,而不是执行 HttpResponse.Redirect。

回答by Karl Anderson

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:

您需要让您的 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);
}

回答by crazy coder

Try this:

尝试这个:

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


        }