PdfResult a custom ActionResult in ASP.NET MVC2 Updated

5 Comments

I was attempting to add the functionality of a pdfresult into my site to generate pdfs in MVC, and I stumbled over this great post by Jim Zimmerman but it didnt work for me as I believe some changes that have come in MVC 2 have broken it. I have made some changes below that will get to work if anybody else needs it go for it.

public static string CaptureActionHtml(
this Controller controller,
TController targetController,
string masterPageName,
Func action)
where TController : Controller
{
if (controller == null)
{
throw new ArgumentNullException(“controller”);
}
if (targetController == null)
{
throw new ArgumentNullException(“targetController”);
}
if (action == null)
{
throw new ArgumentNullException(“action”);
}
// pass the current controller context to orderController
var controllerContext = controller.ControllerContext;
targetController.ControllerContext = controllerContext;

// replace the current context with a new context that writes to a string writer
var existingContext = HttpContext.Current;
var writer = new StringWriter();
var response = new HttpResponse(writer);
var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
HttpContext.Current = context;

// execute the action
var viewResult = action(targetController);

// change the master page name
if (masterPageName != null)
{
viewResult.MasterName = masterPageName;
}

// we have to set the controller route value to the name of the controller we want to execute
// because the ViewLocator class uses this to find the correct view
var oldController = controllerContext.RouteData.Values[“controller”];
controllerContext.RouteData.Values[“controller”] = typeof(TController).Name.Replace(“Controller”, “”);

// execute the result
viewResult.ExecuteResult(controllerContext);

StringWriter sw = new StringWriter();
var viewContext = new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary(controllerContext.Controller.ViewData.Model), new TempDataDictionary(), sw);

viewResult.View.Render(viewContext, HttpContext.Current.Response.Output);
response.Flush();

// restore the old route data
controllerContext.RouteData.Values[“controller”] = oldController;

// restore the old context
HttpContext.Current = existingContext;

return sw.ToString();
}

Advertisement

Getting Javascript Intellisense to work in Visual Studio 2008

Leave a comment

I was very surprise about the amount of incorrect or rather half truth information that was floating around the web to geth javascript intellisense to work in Visual Studio 2008, so once I finally got it working then I just felt I needed to make a quick blog post that may help others in the future.

The main problem occurs when you are within a page that inherits from a master pages, and its that master page that references the javascript files you would like to have the intellisense to reference.

The first step and this is pretty easy to find around the web and thats make a reference comment within your javascript block to the javascript file ie

///<reference path="ext/adapter/ext/ext-base.js" />
///<reference path="ext/ext-all.js" />
///<reference path="ext/ext-all-debug.js" />

Now it still doesnt work and frustration will set in, the secret source to make it work ? hit Ctrl-Shift-J on each of the reference that you would like to start appearing in the javascript intellisense. So simple but so easy once you know how.

Of course you must also have VS2008 SP1 installed and this hotfix if your referencing a -vsdoc.js file as most blogs talk about.

Hope this helps some people out.