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();
}

Advertisements

C# MVC Developers needed

2 Comments

I work with some very exciting well funded startups and they need help to develop some great web sites so I need to find 3 developers. The positions are to work virtual, so you can throw away the commute and work from home. Can provide a flexible casual work environment of 3-4 day work week for the right developer if thats what you want.

You will need to be able to prove some good developer knowledge of C#, a track record to meet deadlines and ability work in an agile development model.

Remuneration is based on your skills so if your interested then drop me a note via www.foundry38.com

Getting to know … Mark Burch

Leave a comment

I had a chat with the winner of the Bizspark Azure Startup camp Mark Burch, he talked about what it was like being part of the Bizspark program and what he has been up to.

BLOW: Networking is gold but dont over do it

Leave a comment

The business lesson of the week for me is how important it is to have a great network of peers and friends around you and also how its important to keep communication open to these relationships. The problem though is the methods of keeping relationships strong such as one on one meetings dont scale, yet you cant build strong relationships with a mass mailout email. I guess the solution is divide your network into must haves and prioritise keeping stronger relationships with those and keep the others at a arms length.

Also make sure you have a system of noting people that you haven’t communicated with for a while and that helps remind you to drop them a line, its always great to receive an email from an old friend and an opportunity to catch up with them.

Mum once said too much of anything is a bad thing, which as general comments go is a very true one. I hate when I go to an event like a usergroup meeting and there is a obvious disguised action that is just getting people to promote up themselves and getting me to whore out my network. A good example of this is godaddy when emails arrived I used to read them, then I realised you get one every couple of days I dont really care or notice them because the noise is to high, so simply put always respect your network and dont over promote yourself.

BLOW: Late for meeting etiquette

Leave a comment

When your late for a meeting it sends the signal that you dont care, so a late meeting should be avoided at all costs. I always plan to be there 15 – 30 mins early so to avoid late meetings and just because your early it doesnt mean its time wasted. The early time can be used to think about the meeting ie plan what you want out of it and what a successful meeting outcome would be, mobile email means the time can be used for email triage, maybe make a call to some people you have been meaning to catchup with or just take a breath and think about life.

Life does throw you curve balls so being late is something that can still happen, but always ring as soon as you know you will be late so the person is not waiting and not wasting their time, it gives a chance to reschedule because a rushed meeting is a bad meeting and if its a lunch meeting you should always be picking up the bill to show the person that you value their time.

One other final thing, if your in a meeting dont answer your mobile phone, if there is a must take call coming through then have the respect of warning the other people that your expecting a call and that its important

Love to hear other suggestions

I will be judging you at Bizspark Startup Camp

Leave a comment

I will be judging you … well if you are entered into the Bizspark Azure Startup camp that is on the weekend of 10th/11th April.

I hope to see lots of great ideas and then see someone walk away with a cool $5000 that can help them bring their idea to market.

BLOW: Keeping focused

Leave a comment

I thought it was time for me to get back to some old routines and one thing I enjoy doing is reflecting over the week that has been and think about where I can improve. I used to do this with a weekly blog post more for my own self growth then any value to others, but none the less people have commented that they have found things helpful.

I have a weakness that I tend to jump around from one project to another, as I get bored with the job at hand I find other things to do. This week I caught myself pressing “New Project” within visual studio, just for an hour I told myself … I quickly caught myself and put myself back to the deadlines and tasks at hand. The problem is I caught myself doing it three times in a day each time with a self made excuse such as “I could use this to build my skills”.

The solution is to set yourself short goals that are achievable in the day and celebrate the victories, reaching a goal makes the slog will worth it

Older Entries Newer Entries