Web.thod中的Response.Redirect

时间:2014-11-21 17:49:31

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

我正在尝试在调用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方法将其发送到服务器端。

2 个答案:

答案 0 :(得分:1)

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

document.location.href = url;

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

"eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia",返回此值。

答案 1 :(得分:0)

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