【问题标题】:Response.Redirect in a WebmethodWeb 方法中的 Response.Redirect
【发布时间】:2014-11-21 17:49:31
【问题描述】:

我正在尝试在调用 webmethod 中使用 response.redirect 命令,但它不起作用,看看我的代码如何:

HTML:

<a onclick="proximaAula()">Próxima Aula -></a>

JS:

 function proximaAula() {
    var tipo = getParameterByName('t');
    if (tipo == "1") {
        //PageMethods.MyMethod(projekktor('player_a').getPosition(), projekktor('player_a').getDuration(), getParameterByName('codaula'));
        PageMethods.NextAula(projekktor('player_a').getPosition(), projekktor('player_a').getDuration(), getParameterByName('codaula'));
    }
    //alert(tipo);
    if (tipo == "3") {
        var iframe = document.getElementById('viewer');
        var iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
        var pag;
        var npag;
        if (iframeDocument) {
            elem = iframeDocument.getElementById('pageNumber').value;
            npag = iframeDocument.getElementById('numPages').innerHTML;
            npag = npag.substring(3, npag.length);
        }
        //return "asasdas"; // 
        //alert(elem + " - " + npag);
        PageMethods.NextAula(elem,npag,getParameterByName('codaula'));
    }
    if (tipo == "4") {
        PageMethods.NextAula("-1","-1",getParameterByName('codaula'));
    }
}

C#:

[WebMethod]
    public static string NextAula(string tempo, string tempomax, string codaula)
    {
        escolawebEntities DB = new escolawebEntities();
        string link = "";
        int icodaula = int.Parse(codaula);

        eadaulaaluno eaula = (from x in DB.eadaulaaluno where x.codeadaula == icodaula select x).FirstOrDefault();

        tempo = tempo.Substring(0, tempo.IndexOf('.'));
        tempomax = tempomax.Substring(0, tempomax.IndexOf('.'));

        int ntempo = ((int.Parse(tempo) * 100) / int.Parse(tempomax));

        if (tempo == tempomax || eaula.percentual == eaula.percentualmax || ntempo >= 95 )//ou perc ser maior 98%
        {
            eadaula aaula = (from x in DB.eadaula where x.eadcodaula == icodaula select x).FirstOrDefault();
            eadaula paula = (from x in DB.eadaula 
                             where x.eadcodcursomodulo == aaula.eadcodcursomodulo
                             where x.ordem == aaula.ordem + 1
                             select x).FirstOrDefault();

            if (paula != null)
            {
                //link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia;
                HttpContext.Current.Response.Redirect("eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia,false);
                //Context.Response.StatusCode = 307;
                //Context.Response.AddHeader("Location", "<redirect URL>");

               // HttpContext.Current.Response.StatusCode = 307;

               // HttpContext.Current.Response.AddHeader("Location", "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia);
            }
                //HttpContext.Current.Response.Redirect("eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia);
                //link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia;
        }
        return "";
    }

我无法使用命令重定向页面,我尝试了几种方法,例如通过字符串返回链接等...

这段代码的工作原理如下:我点击链接,它调用一个javascript函数,该函数获取页面某些组件的一些信息,并通过web方法将其发送到服务器端。

【问题讨论】:

  • 您是在尝试从 JS 调用 Web 服务吗? Web 服务没有 Response.Redirect。
  • 不要让 WebMethod 重定向页面,而是尝试返回您想要重定向到的地址作为响应的一部分,然后让客户端读取响应以获取要重定向到的 URL,然后让客户端访问新的 URL。
  • Change this line in your code PageMethods.NextAula(elem,npag,getParameterByName('codaula'));` 到 var strRedirect = PageMethods.NextAula(elem,npag,getParameterByName('codaula')); 然后在您的 WebMethod 中将 return 从 return "" 更改为返回实际的字符串值分配给局部变量
  • 所以分配link = "eadVerAula.aspx?codaula=" + paula.eadcodaula + "&amp;t=" + paula.eadcodmidia"; then return "" 应该是return link
  • var strRedirect = PageMethods.NextAula (elem, NPAG, getParameterByName ('codaula'));返回未定义

标签: javascript c# asp.net webmethod response.redirect


【解决方案1】:

如果您正在调用异步方法,或者根据网站上的值进行重定向,您可以使用此行来重定向网站:

document.location.href = url;

不要在服务器代码上使用重定向,而是直接从[WebMethod] -NextAula 发送链接并在 js 端使用它。

"eadVerAula.aspx?codaula=" + paula.eadcodaula + "&amp;t=" + paula.eadcodmidia",返回这个值。

【讨论】:

  • 但是在客户端JS不期望webmethod完成,JS运行webmethod并继续下一行
  • 在你的 JS 中使用这个:
【解决方案2】:

网络方法不能使用 Response.Redirect 将网站重定向到不同的地址。它将向调用者发送回一个 XML 响应。尝试从 web 方法发送所需的信息,然后使用它来重定向您的页面。

【讨论】:

  • 我需要服务器检查数据库中的信息,但是在不触发回发的情况下执行此操作的唯一方法是使用 webmethod,就像我对 webmethod 为客户端返回数据所做的那样?
  • @ISFO,这不是真的。 WebMethod 有几种替代方法(不再受支持)。 Web API 是目前最容易使用的一种。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 2018-11-13
  • 2018-06-16
相关资源
最近更新 更多