Search code examples
asp.net-mvcpdfrotativa

Rotativa - controllercontext is null


i work with ASP.NET MVC and i try to use rotativa for build a pdf by model. The problem is that in this context i operate in a model (i can't move the code in controller), and i write a procedure into a controller for build pdf. Like this

        MODEL:

        ModelNIR modelloNIR = new ModelNIR();
        modelloNIR.ufficio = Ufficio;
        if (r != null)
        {
            modelloNIR.ruolo = r.Descrizione.ToUpper();
            //pdfFormFields.SetField("RUOLO", r.Descrizione.ToUpper());
            //pdfFormFields.SetField("RUOLO2", "RUOLO " + r.Descrizione.ToUpper());
        }


        if (atto.IdTipoParte == 1)
            modelloNIR.convenuto = 1;
        if (atto.IdTipoParte == 2)
            modelloNIR.ricorrente = 1;


        if (atto.IdTipoAtto == 1)
            modelloNIR.citazione = 1;
        if (atto.IdTipoAtto == 2)
            modelloNIR.altro = 1;
        if (atto.IdTipoAtto == 3)
            modelloNIR.ricorso = 1;

        if (Contributo != "")
            Contributo = "Euro " + Contributo;
        else
            Contributo = "Esente";

        modelloNIR.valoreCausa = "Euro " + valoreCausa;
        modelloNIR.contributo = Contributo;
        modelloNIR.oggetto = oggetto;

        if (ControparteNew == "") ControparteNew = Controparte;
        modelloNIR.promosso = Promossoda;
        modelloNIR.contro = ControparteNew;

        NotificheMezzoPecsController not = new NotificheMezzoPecsController();
        // call controller action
        MemoryStream nir = not.createPDFNIR(modelloNIR);

And this is the controller's action

        CONTROLLER:
        public MemoryStream createPDFNIR(ModelNIR modelloNIR)
    {
        var actionPDF = new ViewAsPdf("NIR", modelloNIR) //some route values)
        {
            FileName = "NotaIscrizioneRuolo.pdf"
            //CustomSwitches = custom,
            //PageSize = 4
        };

        byte[] applicationPDFData = null;
        try
        {
            applicationPDFData = actionPDF.BuildPdf(ControllerContext);
        }
        catch (Exception ex)
        {
            throw;
        }
        //Memory
        PdfUtilityCommon utility = new PdfUtilityCommon();
        List<byte[]> listaPDF = new List<byte[]>();
        listaPDF.Add(applicationPDFData);
        MemoryStream ms = utility.MergePdfForms(listaPDF);
        return ms;
    }

but i have a runtime error because the controllercontext is null Anyone can help me?


Solution

  • If you take a look at ControllerContext class documentation, it is expected to has HTTP request information. As you have instantiated this controller, there is no related HTTP request, so the context has no data.

    Maybe you could try building your own HTTP data on this ControllerContext class. Please, try using this code:

    NotificheMezzoPecsController not = new NotificheMezzoPecsController();
    
    RouteData route = new RouteData();
    route.Values.Add("action", "createPDFNIR");
    route.Values.Add("controller", "NotificheMezzoPecsController");
    
    ControllerContext newContext = new ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, not);
    not.ControllerContext = newContext;
    
    // call controller action
    MemoryStream nir = not.createPDFNIR(modelloNIR);
    

    Let me know if it works for you.