TempData works on Localhost but does not work when published

1

Thank you for your attention!

I'm developing in ASP.NET MVC5

What happens is this: When you run tests on 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 to an approval server, the call to the Controller BAIXARPDF can not receive the tempdata , it is null. Can anyone give me an idea of why different behavior in environments?

I make the following Ajax call:

        $.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 invokes Action in 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);
    }

This Controller mounts a tempdata and returns a Json with the URL to the next Controller that will retrieve the object within 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);
       }
    }
    
asked by anonymous 12.08.2015 / 15:09

1 answer

1

This is the wrong way to do it. First, there is no need to separate the code in this way, because each Action of a Controller necessarily needs to return a concrete result. You've set up an Action that does not work on its own, and depends on another to return something you want.

Secondly, that's not why TempData works. It saves some information that may be useful for building a View , and just that. Not for mounting PDFs and stuff.

Third, using Ajax to return a file is totally wrong . I have no idea why you're doing this, but Ajax does not handle this type of request. The correct one is to use a normal request and make your Controller return a FileResult .

To the changes.

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

Second, change your BaixarPDF method 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"
    };
}

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

    
12.08.2015 / 16:55