Ian Obermiller

Part time hacker, full time dad.

MVC error: “Cannot create an abstract class”

While debugging an MVC4 app, I ran into the following error: System.MissingMethodException: Cannot create an abstract class. It was only happening for a single Action, and I wasn’t able to set a breakpoint in that action. After some trial and error, I was able to find the issue in the code:

public virtual ActionResult Login(ActionResult redirectAction = null)
{
    ...
}

The goal was to have an T4MVC style helper for passing in an ActionResult instead of an untyped path. This was a bad idea, as ActionResult is an abstract class. When I hit http://localhost:2995/Account/Login, the MVC framework tried to instantiate ActionResult to fill in the default value for redirectAction. I simply reverted the parameter back to a string, and all was well.

public virtual ActionResult Login(string redirectPath = null)
{
    redirectPath = redirectPath ?? Url.Action(MVC.Home.Index());
    ...
}

Type or namespace does not exist when compiling MVC View

I ran into the following common error when trying to run my ASP.NET MVC 4 website:

d:\temp\2pirqzul.5yz\temp\root\898a687c\8681620c\App_Web_jfuizczv.0.cs(27): error CS0234: The type or namespace name 'Models' does not exist in the namespace 'DealBoxy' (are you missing an assembly reference?)

While normally a straightforward error, this one was confusing, because I was certain that I had referenced the DealBoxy project correctly.

It turns out that DealBoxy was compiling to an exe, and apparently ASP.NET won’t load exes the same way it will dlls. Changing the Output type to Class Library in the Application tab of the project settings did the trick.

Require HTTPS on Azure

Once you have configured an Azure web role with an SSL certificate and setup the port configuration in the Azure project, you may want to redirect anyone who comes to the http:// version of your page to the secured https:// version. To do so in production, add the following to your Web.Release.config:

(more…)