Controllercontext Null

Asked

Viewed 262 times

2

I have the following code:

PDFController Ctrl = new PDFController();
            byte[] ArquivoPDF = Ctrl.GerarPDF(xml); 

This part above is in the Webapi, where I create a new instance of the Controller to use the function that is in it. The code below is already part of the controller:

public class PDFController : Controller
{
 public string RenderizaHtmlComoString(string NomeView, object model)
    {
        ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, NomeView);

            var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

            return sw.GetStringBuilder().ToString();
        }
    }
}

But in the part that I will form the variable viewResult, it gives an error and says that the Controllercontext cannot be null. Can someone help me? *This Controller is not linked to any View.

1 answer

2


Your Controllercontext in this case will always be null because you are instantiating the controller "manually".

Usually the controller is created by an Icontrollerfactoy that is usually Dafaultcontrollerfactory, it creates the controller with all the necessary dependencies that includes the Controllercontext.

Usually when common methods are needed between controllers, a base controller is created that is inherited from the other controllers. In your case it would look something like this:

    public class BaseController : Controller
{
    // GET: Base
    public byte[] GerarPDF(string content)
    {
        //Código que gera o pdf.

        return new byte[] { };
    }
}
    public class ControllerA : BaseController
{
    public byte[] AlgumMetodo()
    {
        return GerarPDF("");
    }
}

public class ControllerB : BaseController
{
    public string AlgumOutroMetodo()
    {
        byte[] pdf = GerarPDF("");

        return "";
    }
}
  • I get it, but in case I need to call these controller functions from within an Api, would you know how to do that? was even for that reason that I created an instance of the controller, to try to do with q I managed to call the method.

  • Actually in this case are controllers of different types. In this case I would put this method in a separate class to be used in the controller. I don’t know how the code is, so I don’t know if it’s possible because of some dependency.

  • So what’s stopping me is that this PDF q method I posted, it returns a view, only, I can even return the view even outside the controller, only it contains some Tempdata and some Viewbag for its assembly to be complete. Then I find another problem, that I did not find a way to use the Tempdata and Viewbag outside the controller... Unless you have some way to replace them with other options I’ve never heard of.

  • Well I really think you have a problem with architecture there... this method is very attached to an environment, which in this case is the web since Temdata and Viewbag are usually used to pass information to view. In this case requires further analysis to decouple it and separate it into a class responsible for it, principle of sole responsibility (SOLID).

  • Yeah, you’re in a little bit of a bind, it’s just that I already had this awareness that I was going to have to move the structure, but I was hoping that there was some way that it wasn’t necessary to make these changes, but thank you I will begin to study the structure to make the adjustments. Thank you :D

  • Good luck there, and with calm you can separate it for sure. ;)

Show 1 more comment

Browser other questions tagged

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