.NET Development on Mac

What did you say?

Just a couple of short years ago, the idea of using a Mac for .NET development would have sounded completely insane, but much has changed.  The .NET Framework has since been open sourced and Microsoft has been clear about its intent to make .NET development cross-platform, which it has achieved with the release of ASP.NET 5.

Enter Visual Studio Code

It’s no Visual Studio 2015, but Visual Studio Code is an Electron-based editor for .NET along with support for a plethora of other languages.  It has a debugger, intellisense and many other features that you would expect from Visual Studio.  While I’m sure that their will be a full, cross-platform version of Visual Studio in the future, Visual Studio Code is a great option for Mac and Linux users.  (Tip:  If you ever need to do a web search for Visual Studio Code related content, search for VSCode instead.  That will return results specific to Visual Studio Code, rather than Visual Studio.)

Staying Connected

Visual Studio Code has git support built in, but if you’re like me then you also do work on .NET projects over FTP.  While Visual Studio provides FTP support, VSCode does not at this time and plugin support is still coming in a future release.  I have found a great option for using VSCode over FTP with the help of Transmit.  Transmit will allow you to mount an FTP site as a drive and then VSCode can open that drive as a working folder and take over from there.  Transmit will take care of transferring files in the background for you.  It is an all around great FTP client.

Where’s the Remote?

As a part of my switch to the Mac, I was almost embarrassed to be discovering so late that Microsoft Remote Desktop is so much better on the Mac than it is on Windows.  Remote Desktop has a much better way of managing saved connections than the simple drop down list on the Windows version.  My favorite feature by far is that each open connection displays as a new desktop in Expose, making it very easy to manage all of your open desktops.

For All Else, There’s Virtualization

Sometimes you just have to work in Windows.  There’s no way of getting around it and Windows 8.1 is great and only getting better with Windows 10, so why should you?  Using Boot Camp you can install Windows on a separate partition on your Mac, but then you need to completely shut down your session in OS X when you need to do work in Windows.  These days that is just not necessary as virtualization is as good as working on a machine natively.  With Windows installed in VirtualBox, an adequate amount of memory assigned and the VM in full screen mode, I can hardly tell the difference between working directly on a Windows machine.

I’d love to hear about your experiences moving to the Mac for development.  Leave a comment below.

Paste XML as Classes Missing In Visual Studio

Just wanted to pass along a quick tip for working with the Paste Special features in Visual Studio.  You may be aware that Visual Studio has options for generating classes based on XML and JSON under the Edit, Paste Special menu.

Paste Special

The Paste Special menu and options in Visual Studio 2013.

These options will generate VB.NET or C# classes that work with the built-in .NET serialization libraries.  However, you may only see the option to “Paste JSON as Classes” from the menu in your Visual Studio window.  This is because XML option is only available in projects targeting .NET Framework 4.5 and above.  Change your project’s target framework version and the option will become available.

If you are not able to upgrade your project, you could always create a new .NET 4.5 project, generate the classes there and then copy them to the destination.  You may need to correct some version specific errors, but this option will still get you most of the way there.

System.Web.WebPages conflict between ASP.NET MVC 3 and ASP.NET MVC 4

After installing ASP.NET MVC 4, you may notice that you are unable to load projects that used previous versions of ASP.NET MVC. You may see an error similar to the following:

The type ‘System.Web.Mvc.ModelClientValidationRule’ exists in both ‘c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies\System.Web.Mvc.dll’ and ‘c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.WebPages.dll’

This error occurs because prior to MVC 4, projects did not specify which version of ASP.NET Web Pages to use, since there was only one. Luckily, the fix for this is as simple as updating the project file to the proper reference of System.Web.WebPages. Many developers have not ever needed to modify a project file directly, since the default Visual Studio configuration is often sufficient. This can make editing a project file an intimidating task, so I have assembled detailed steps to solve the problem.

From the Solution Explorer, right click on the project and click “Unload Project” from the context menu.

The "Unload Project" menu item in Visual Studio 2010.

The “Unload Project” menu item in Visual Studio 2010.

You should see that the project is now marked with (unavailable). Right click on the project again and you should be able to select a new item, “Edit {Your Project Name}.csproj”. The extension will be displayed as .vbproj for VB.NET projects.

The "Edit Project" menu item in Visual Studio 2010.

The “Edit Project” menu item in Visual Studio 2010.

An XML configuration file will open. This contains all of the information that tells Visual Studio how to run your project. Scroll down (or search the file) until you find the following line:

<Reference Include="System.Web.WebPages"/>

Replace that entire line with this:

<Reference Include="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"/>

Save the changes to the project file. Right click the project name in the Solution Explorer again. This time, select “Reload Project”. Your project will reopen in Visual Studio and should now build and run properly.

In simple terms, what we have done here is update the Reference tag to include version information, which resolves the conflict for Visual Studio.  It now knows which version of System.Web.WebPages you intend to use for your project.

Thanks for reading.  Still having trouble resolving the error?  Feel free to leave me a comment below!