Rotativa - controllercontext is null

假如想象 提交于 2021-01-28 18:56:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/63508982/rotativa-controllercontext-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!