【问题标题】:can't call Response.Redirect inside a static method不能在静态方法中调用 Response.Redirect
【发布时间】:2013-06-09 00:42:34
【问题描述】:

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

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

visual studio 用红线标记它:“非静态字段方法或属性 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 href:

<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) 没有静态Response。 2)无论如何,这不会是你想要的。您需要与 JS 交谈。
  • 我试图和它说话,它不会回答 :) 你这是什么意思?
  • 你需要返回一个告诉 JS 做什么的结果。
  • 你为什么使用 AJAX? 听起来你想要链接点击导致浏览器加载不同的页面。如果您只想加载一个新页面,AJAX 是一个糟糕的选择。

标签: c# javascript asp.net ajax static-methods


【解决方案1】:

试试这个:

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】:

您需要让您的网络方法传回您想要重定向到其个人资料的用户的 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);
}

【讨论】:

    【解决方案3】:

    您需要将构建的 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 似乎获得了字符串值...
    • res.d 是假的.. 不是字符串
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 2023-02-22
      • 2018-02-23
      • 2011-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-16
      相关资源
      最近更新 更多