Tempdata works on Localhost but doesn’t work when published

Asked

Viewed 282 times

1

Thank you in advance for your attention!

I am developing in ASP.NET MVC5

What happens is this: when the tests are carried out in the localhost, this code works perfectly. I can retrieve the tempdata containing the object to send it to the View that generates the PDF. However, when I publish the project on an approval server, the call to the Controller BAIXARPDF cannot receive the tempdata, it is null. Someone can give me an idea of why the different behavior in the environments?

I make the following call Ajax:

        $.ajax({
            url:urlL,
            async: false,
            type: "POST",
            data: JSON.stringify(UniformePDF),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (iddata) {
                window.location = iddata;
                baixarPDF(iddata);
            },
            error: function (xhr, textStatus, err) {
                alert(err);
            },
            cache: false
        });
    });

This call calls the following Action in the Controller:

    public ActionResult GerarPDF(UniformePDF u)
    {
        TempData["teste"] = "teste";

        if (u.NomePeca == null)
        {
            u.NomePeca = "padrao";
        }

        if (Request.IsAjaxRequest())
        {
            var urlHelper = new UrlHelper(Request.RequestContext);
            ViewBag.UniformeBag = u;
            TempData[u.NomePeca] = u;
            TempData.Keep(u.NomePeca);
           //string url =  RedirectToAction("BaixarPDF", "Uniforme", new { UniformePDF = u, area = "Uniformes"})
           string url = urlHelper.Action("baixarPDF", new { idUniforme = u.NomePeca });
            return Json(url, JsonRequestBehavior.AllowGet);

        }
        return Json("OK", JsonRequestBehavior.AllowGet);
    }

That one Controller rides a tempdata and returns a Json with the URL to the next Controller that will retrieve the object inside the tempdata and return a View that generates a PDF.

  public ActionResult baixarPDF(string idUni)
  {
       bool retorno = false;
       String remover, link, adicionar;
       UniformePDF modeloUniforme = new UniformePDF();

       if (TempData["teste"] != null)
       {
           modeloUniforme = TempData[Request.QueryString["idUniforme"]] as UniformePDF;
           if (modeloUniforme.NomePeca == Request.QueryString["idUniforme"])
           {
               //var svg = Encoding.UTF8.GetString(bytes);
               remover = getBetween(modeloUniforme.Desenho, "<svg", "<g");
               link = "http:" + "//www.w3.org/2000/svg";
               adicionar = "<svg xmlns=\"" + link + "\" width=\"500px\" height=\"500px\" viewBox=\"0 0 1000 1000\">";
               if (remover.Length != 0)
               {
                    modeloUniforme.Desenho = modeloUniforme.Desenho.Replace(remover, adicionar);
               }

               string svgString = modeloUniforme.Desenho;
               string base64 = CreateBase64Image(svgString);
               modeloUniforme.DirBase64 = base64;
                retorno = true;
            }
        }
       if (retorno)
       {
           return new PartialViewAsPdf("Modelo", modeloUniforme)
           {
               PageSize = Rotativa.Options.Size.A4,
               FileName = modeloUniforme.NomePeca + ".pdf"
           };
       }
       else
       {
           return Json(retorno, JsonRequestBehavior.AllowGet);
       }
    }
  • In the console from the browser you receive some error while calling the query ajax? What value is being passed on urlL?

1 answer

1

This is the most wrong way to do it. First of all, there is no need to separate the code in this way, because every Action of a Controller necessarily need to return a concrete result. You mounted a Action that does not work alone, and that depends on another to return something desired.

Secondly, that’s not why TempData serves. It stores some information that may be useful for building a View, and just that. It’s not for mounting Pdfs and things like.

Third, use Ajax to return a file it’s totally wrong. I have no idea why you are doing so, but Ajax does not handle this type of request. The correct is to use a normal request and make your Controller return a FileResult.


To the amendments.

First, GerarPDF can be entire thrown away. Everything it does in the context of what you need is useless.

Second, change your method BaixarPDF to the following:

[HttpPost]
public ActionResult BaixarPDF(UniformePDF uniforme)
{
    // Retirei essas variáveis daqui. Essa declaração não tem necessidade.
    // String remover, link, adicionar;

    //var svg = Encoding.UTF8.GetString(bytes);
    var remover = getBetween(uniforme.Desenho, "<svg", "<g");
    var link = "http:" + "//www.w3.org/2000/svg";
    var adicionar = "<svg xmlns=\"" + link + "\" width=\"500px\" height=\"500px\" viewBox=\"0 0 1000 1000\">";

    if (remover.Length != 0)
    {
        uniforme.Desenho = uniforme.Desenho.Replace(remover, adicionar);
    }

    var svgString = modeloUniforme.Desenho;
    var base64 = CreateBase64Image(svgString);
    modeloUniforme.DirBase64 = base64;

    return new PartialViewAsPdf("Modelo", modeloUniforme)
    {
        PageSize = Rotativa.Options.Size.A4,
        FileName = modeloUniforme.NomePeca + ".pdf"
    };
}

How are you using Rotary, use the ActionResult because the MVC will have to generate the View for you. FileResult would be for the case where the PDF is mounted on Controller.

  • Gypsy, thank you very much for your explanation, I performed an ajax call to generate the pdf because I needed to keep the status of the page static, it was a "engambiarrada" way to solve the problem, the case was that it worked normal, I was calling with ajax to assemble an object with data that is built inside an SVG. I had to assemble an object and send it to a controller, and saved the objects inside the TEMP, because it was the only way I could see to save the built object to send to the rotary

Browser other questions tagged

You are not signed in. Login or sign up in order to post.