Response.Redirect in a Webmethod

2.7k views Asked by At

I am trying to use the response.redirect command in call webmethod and it does not work, see how is my code:

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

I can not use a command to redirect the page, I've tried several ways, such as the return link via string etc ...

This code works as follows: I click the link, it calls a javascript function, this function takes some information of some components of the page and sends it to serverside by a webmethod.

2

There are 2 answers

2
Piyush Parashar On

A webmethod can not redirect a website to a different address using Response.Redirect. It will send an XML Response back to the caller. Try to send required information from the webmethod and then use it to redirect your page.

2
RobertoNovelo On

Either if you are calling an async method, or redirecting based on values on the website, you can use this line to redirect the website:

document.location.href = url;

Instead of using redirect on your server code, send the link directly from [WebMethod] -NextAula and use it on the js side.

"eadVerAula.aspx?codaula=" + paula.eadcodaula + "&t=" + paula.eadcodmidia" , return this value.