Thoughts on Software

Archive for the ‘ASP.Net MVC’ Category

Unsupported Project Type: Why won’t my Visual Studio project load?

leave a comment »

One of Visual Studio’s least helpful habits is that when you open a solution and one of the projects cannot be loaded, it simply gives you the unhelpful message the the project is of an unsupported type. Great. So what to do next?


The basic problem is that you are missing an indeterminate something that Visual Studio requires to load that project. For example, if the project type is MVC 3, you need MVC 3 installed on the machine you are opening the project on. Unfortunately, Visual Studio won’t tell you which project type it can’t load …


There are a couple of steps required to resolve this:

  1. Find out which project type is missing
  2. Install the missing software and reload


The information as to which project type is missing can be discovered from the .csproj file for the non-loading project. First, open the project in Notepad or similar and look for the ProjectTypeGuids tag:



This tag contains the Project Type GUIDS: ie, the identifiers which tell us what project types might be causing our exception. Now we need to work the friendly names for each GUID. I generally use this article as a starting point as it contains many of the common GUIDs:


Search through this page for each GUID in turn:

{E3E379DF-F4C6-4180-9B81-6769533ABE47} = ASP.Net MVC 4
{349c5851-65df-11da-9384-00065b846f21} = Web Application
{fae04ec0-301f-11d3-bf4b-00c04f79efbc} = Windows (C#)

In this case, the culprit is likely ASP.Net MVC 4 and sure enough, I had MVC 1, 2 and 3 installed but not 4 and so the fix is simply to download and install MVC 4.







Written by andrewlocatelliwoodcock

April 22, 2014 at 16:19

Modern Corporate R&D

leave a comment »

I am based these days in Citi’s research lab in Dublin working on financial mobile solutions. A few weeks ago, Irving Wladawky-Berger came to the Lab to see what we are up to. I didn’t get to meet him unfortunately but he wrote a very interesting blog post on corporate R&D and how it has evolved over the years to meet accelerating change in the outside world and was very positive about Citi’s Dublin lab:
Personally, I like the sound of the old-style labs where we just got on with the interesting bits and left Marketing to figure it all out 😉

Written by andrewlocatelliwoodcock

September 6, 2012 at 09:32

Posted in ASP.Net MVC

How to distinguish HTTP POST action methods at runtime: Checking HTTPContext Server Variables

leave a comment »

Following on from my last post, there is an alternative method for distinguishing whether we are dealing with an HTTP POST. Before, I suggested checking the ActionDecriptor to see whether there was an HttpPost attribute applied. An alternative to this is to check the ControllerContext for the REQUEST_METHOD server variable. If this is “POST”, then this is an HTTP POST.


public static bool IsHttpPost(ControllerContext context) 
    return context.RequestContext.HttpContext.Request.ServerVariables.Get("REQUEST_METHOD").ToUpper() == "POST"; 


Written by andrewlocatelliwoodcock

January 26, 2012 at 18:03

How to distinguish HTTP POST action methods at runtime

with one comment

I had a requirement recently to only apply an action filter to HTTP POST action methods on the controller. So the task was to create a conditional filter provider (which I will cover in a later post) which would only return the attribute if the action also had the HttpPostAttribute applied.

It turns out that it is actually quite simple to discover whether the HttpPostAttribute has been applied if you have access to an ActionDescriptor object, which you can get at from the FilterContext object available to filter attributes in their OnActionExecuting, OnActionExecuted, OnResultExecuting and OnResultExecuted methods as well as in the filter provider. Then it’s just a case of using the GetCustomAttributes and a bit of LINQ to find out if any of the action’s attributes are an HttpPostAttribute:


public static bool IsHttpPost(ActionDescriptor action)
    return action.GetCustomAttributes(typeof(HttpPostAttribute), true).Any();


Written by andrewlocatelliwoodcock

January 23, 2012 at 21:11

Accessing Model validation errors from the Controller

with 16 comments

MVC performs automatic model validation whilst it is binding the model, so as soon as we hit the Controller action method that is being called, we have access to a validated model. In fact, as discussed in my last post, we actually have access to the results of model validation in the method’s ActionFilters, including the first ActionFilter: OnActionExecuting, which fires before we even reach the action method.

MVC Exposes the results of this validation through the Controller’s ViewData.ModelState property. This is directly accessible in the Controller by calling ModelState and is most often used to check whether a model passed validation:


if (ModelState.IsValid)
    // model passed validation, so do some work here


But if ModelState.IsValid returns false, ie the model failed validation, how can we see the collection of errors?

Read the rest of this entry »

Written by andrewlocatelliwoodcock

December 16, 2011 at 20:17

Canceling the ActionExecutingContext in the OnActionExecuting ActionFilter

with 5 comments

I have been working recently with ActionFilters in ASP.Net MVC and was trying to prevent processing of a Controller action method from within the OnActionExecting action filter. As usual, MVC supports this level of customization, but it was not immediately obvious how to achieve it.

Specifically, I don’t like the boiler plate code required by the standard approach to only processing an action method if the model is valid and I wanted to replace it with a custom action filter instead.

Read the rest of this entry »

Written by andrewlocatelliwoodcock

December 15, 2011 at 12:24

How to create an ActionResult from a string

with 2 comments

There are times when for whatever reason you want to return a string from a call to an Action method instead of a formal View; perhaps you want to return a Base64 encoded image or display the contents of a file, for example. ASP.Net MVC actually makes this very straightforward to do but it is not that well documented: use the Content method of the Controller as in the example below:

Read the rest of this entry »

Written by andrewlocatelliwoodcock

November 18, 2011 at 22:13

Posted in ASP.Net MVC, C#

Tagged with

LoggingViewEngine: a concrete example of using the Shaver framework

with 6 comments

As I explained in Shaver View Engine: a more in-depth look, Shaver is a framework for an extensible view engine that allows views to be written in Razor syntax, captures the HTML/CSS output using the Razor view engine and then makes that output available for use via extension points so that the original Razor view can be repurposed to produce any output you want or perform additional tasks prior to returning to the browser, or both.

If the output you want can be produced from HTML, or make use of HTML, Shaver facilitates this but does nothing with the output itself: that task is left up to concrete implementations of the View Engine. Shaver is an abstract View Engine and we need concrete implementations in order to do anything useful.

This post is going to concentrate on a simple concrete implementation of the Shaver View Engine, LoggingViewEngine. The LoggingViewEngine returns the view as normal but first logs the rendered HTML via Log4Net. And all that is required to achieve this functionality is to change the View’s extension to cslog …

Read the rest of this entry »

Written by andrewlocatelliwoodcock

November 17, 2011 at 21:05

Shaver Framework API: ShaverView

with 5 comments

Following on from our in-depth introduction to Shaver and the first part of Shaver’s API, the second part of the API is the ShaverView:

ShaverView is an abstract class that implements the IView interface and provides a set of extension points. Just like ShaverViewEngine, ShaverView cannot be used directly but is intended to provide a framework and API. Have a look at Logging View EngineMailer View Engine and PDF View Engine to see examples of how this is done.

The API is made up of three abstract methods that must be overridden by each implementation (Setup, ActOnHtml and RenderView), a virtual method that may be overridden by an implementation if required (CaptureHtml) and two static helper methods (GetViewData and GetTempData), that extract view and temp data from the controller context.

ShaverView’s constructor accepts two parameters the ControllerContext and an instance of IView. Not co-incidentally, these objects are supplied to ShaverViewEngine’s CreateReturnView method and it is intended that View inheriting from ShaverView will be instantiated in the ViewEngine’s CreateReturnView method. Once again, have a look at Logging View EngineMailer View Engine and PDF View Engine to see examples of how this is done.

The constructor calls the API methods in the following order:

  1. Setup – performs any setup tasks required
  2. CaptureHtml – captures and returns the HTML rendered by the Razor View Engine
  3. ActOnHtml – acts on the HTML returned from the CaptureHtml method
Setup and ActOnHtml, as previously described, are abstract methods and must be implemented by each concrete View Engine using the Shaver framework. CaptureHtml is a virtual method: it provides a default implementation that captures the HTML output generated by the Razor View Engine but can be overridden if necessary. ActOnHtml accepts the HTML returned from the CaptureHtml method as well as the ControllerContext and the IView object and performs whatever custom tasks are required by this particular implementation of the Shaver framework.
The Render method is required by the IView interface and simply calls the RenderView abstract method. RenderView must be implemented by any concrete implementation of the Shaver framework and is intended to perform the rendering tasks required by the particular View Engine.

Written by andrewlocatelliwoodcock

November 8, 2011 at 22:31

Shaver Framework API: ShaverViewEngine

with 4 comments

Following on from the last post, let’s start our discussion of the Shaver API with the Shaver View Engine itself:

There are a couple of things to note: firstly, ShaverViewEngine inherits from RazorViewEngine so that we can re-use and co-opt all the power of the Razor View Engine without re-inventing the wheel: Shaver is about doing unexpected, unenvisioned things with existing Razor syntax not developing a new syntax!

Secondly, there are only two methods: CreateView and CreateReturnView. CreateView overrides Razor’s implementation of the standard CreateView method and uses it to create an IView object which is then passed, along with the ControllerContext to the virtual method CreateReturnView. Custom View Engines that implement the Shaver framework can perform any custom tasks required within their own implementation of CreateReturnView: it is the output of CreateReturnView that is returned to MVC, not the output of CreateView as might be expected. We are letting Razor do the hard work here – it already does it really well, we just want to make it easy to do something different with the output!

CreateReturnView is the first extension point in our Shaver API.

Note also that Shaver itself provides no code to register custom view extensions: Shaver is an abstract View Engine and as such does not provide complete functionality only an API. As such, it makes no sense to attempt to register custom view extensions within the Shaver View Engine itself, this is a task for each concrete implementation such as LoggingViewEngine.

Shaver cannot in fact be used directly: note that it is an abstract class: we need a concrete implementation of the Shaver framework to instantiate – Shaver cannot and does not do the job on its own. Have a look at Logging View EngineMailer View Engine and PDF View Engine to see examples of how this is done.

Next: the Shaver API: ShaverView

Written by andrewlocatelliwoodcock

November 7, 2011 at 20:54