Friday, September 30, 2016

Using FluentValidation for ASP.NET Web API

If you want to implement validation for your ASP.NET MVC Web Application, this is easily done in a number of ways using ASP.NET MVC's own validation mechanisms as well as using NuGet packages such as FluentValidation for ASP.NET MVC:

Unfortunately, even though there is also a FluentValidation NuGet package for ASP.NET Web API (, there is no corresponding documentation for ASP.NET Web API on the FluentValidation Wiki!(

Thankfully, though, there are a few articles which address just how to accomplish this using the FluentValidation library for ASP.NET Web API!

Expand or Collapse Regions in Visual Studio 2015

I frequently work with regions throughout my C# code, so I usually need to expand or collapse all regions in my code especially when I am refactoring code.

Therefore, I wanted to see if there was an easy way to accomplish this within Visual Studio.

Well, by default, Visual Studio uses the Edit-->Outlining menu to control the expanding and collapsing of regions:

However, there are some Quick Launch commands also available to you specifically while working with Regions!

These Quick Launch commands come to you courtesy of the Productivity Power Tools 2015:

Installing this extension adds Quick Launch items for "CollapseRegions" and "ExpandRegions":

In case you are not already aware, the Quick Launch menu appears in the top right hand corner of Visual Studio and allows you to search for various commands available to Visual Studio:

Understanding Attribute-based routing in ASP.NET Web API

If you are new to Web API, you may not completely understand how Attribute-based routing in ASP.NET Web API works.

The documentation for ASP.NET Web API Attribute-based routing is rather sparse and very confusing at times.

Therefore, I thought I would provide some clarifications on Attribute-based routing for anyone who is developing with ASP.NET Web API:

  1. Attribute-based routing, by default, takes precedence over any routing rules defined in WebApiConfig.cs
  2. When defining your Attribute-based routes, there is no way to enforce having all of your routes defined in lowercase or camel-case, therefore, it is best to simply standardize the route name casing your will be using and ensure that the entire development team is aware of this standard and follows it throughout all your development.  A good way to publish these standards is through a file in source control or publishing it on an Intranet Wiki site.
  3. Once you define a [RoutePrefix] attribute at the top of your Web  API Controller class, you MUST define Attribute routes for all of the other methods in your Web API Controller.
  4. For default named methods such as Get, Put, Post and Delete, you will still have to define the default attribute route of [Route("")]
  5. Even when using convention-based naming, it is best to decorate your Web API method names with their appropriate action types such as [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
  6. For methods such as Get and Delete, you usually DO define route parameters.  Therefore, a GetById or a Delete method, might have the following Route attribute defined: [Route("{id}")]
  7. For methods such as Post and Put, you usually DO NOT define any route parameters.  This is because the assumption is that you are posting a model using the [FromBody] attribute, so a Post or Put operation assumes that you are not sending values in the Url.
  8. Any parameters which are not explicitly defined in the route will automatically end up with query-string based routes rather than RESTful routes.  Therefore, it is best to define ALL parameters in your attribute based routes.  Ex: [Route("{id}/details/{detailsid}")]
  9. Parameter names DO make a difference in Web API!  Therefore, if your parameter is named myId, then you need to define a route as follows: [Route("{myId}")].  If you define a route as follows: [Route("{id}")] this will NOT work!
  10. You CAN have the same [RoutePrefix] on multiple Web API Controllers!  Therefore, if you want to split up your functionality across multiple Web API Controllers, you can readily do so and just adjust your route attributes accordingly on each of your Web API methods.
  11. If you have inheritance hierarchies in your Web API controllers, you need to standardize the conventions for your Attribute-based routes such as all Details for a particular class be accessed using a Route such as the following: [Route("{id}/details")]
  12. When performing Get requests which receive a collection of all the items, the standard is to simply have an empty route: [Route("")].  However, if you want to retrieve a specific item, then you should parameterize the route: [Route("{id}")].  These can be distinguished by having method names such as Get and GetById.  
  13. If you install Web API Help or Swagger for your ASP.NET Web API project, you can review all of your routes to make sure they are correct in an easy to view web page, by either navigating from your web browser to either /help or /swagger in the root of your Web API project.  Therefore, you might have a Url such as http://localhost/MyWebAPI/help or http://localhost/MyWebAPI/swagger
That should be pretty much all you need to get started developing your ASP.NET Web API Web Applications using Attribute-based routing!

Thursday, September 29, 2016

Windows Server 2016 RTM Released!

In case you haven't already heard the news, Windows Server 2016 was released!

Even though you cannot yet download it from MSDN, you can download a 180-day evaluation version from here and try it out for yourself:

Tuesday, September 27, 2016

Oracle Database Connection String for ODP.NET using TNSNames.ora

If you are using the TNSNames.ora file for connecting to your Oracle database, you might be a bit confused about using the following Oracle connection string as listed here:

Normally, when you see something like that, you think immediately of setting up a DSN in ODBC Data Sources, right? Well, in this case, you do not need to set up anything in ODBC Data Sources! You simply configure your Net Service Name in your Oracle Client so that you have one or more entries in your tnsnames.ora file as follows:

Now, the Data Source attribute in your Oracle connection string simply corresponds to one of the entries in your tnsnames.ora file (in this case, there are entries for ORCL11G and ORCL respectively).

That is all there is to understanding how to build your Oracle database connection strings using TNS Names!

Oracle Database Connection String for ODP.NET without TNSNames.ora file

I was recently attempting to follow the Oracle database connection string listed on using the ODP.NET Data Provider in order to establish a database connection to my Oracle 11g server, but I was soon faced with this error message:

ORA-12514: TNS: listener does not currently know of service requested in connect descriptor

I couldn't figure out the cause of this problem, since this same connection information is listed in the Oracle documentation (under the section for "Specifying the Data Source Attribute")!

Fortunately, one of my co-workers suggested using the following alternative connection string by replacing SERVICE_NAME with SID:

Making this simple change resolved my connectivity issues!

Using ASP.NET MVC Futures to eliminate Magic Strings

If you have used RedirectToAction throughout your ASP.NET MVC Controllers, you have probably experienced the frustration of having to manually type in string names of controllers as follows:

However, with ASP.NET MVC Futures, you can eliminate typing in these "Magic Strings" for these Redirects and instead rely on strong typing and using lambda expressions!

In order to get started, you first have to install Microsoft.AspNet.Mvc.Futures as a NuGet package:

Once you have installed this NuGet package, you will end up with a reference to "Microsoft.Web.Mvc".  You will have to add a using statement for this library to the top of your MVC Controller file.

Once you have done that, you can easily replace this RedirectToAction functionality in your Controller methods as follows:

That is all there is to cleaning up numerous "Magic Strings" from your ASP.NET MVC Web Application!

Installing Telnet Client from PowerShell

If you need to install the Telnet Client, you can either install it from using the Server Manager GUI or you can install it directly from PowerShell!

I, personally prefer PowerShell since it is much quicker than going through the wizards in Server Manager.

If you want to install Telnet Client from ANY client OS or server OS, you can run the following command:

However, if you wish to install PowerShell on a server OS, you can run the following command:

Monday, September 26, 2016

Where are the 32-bit Oracle ODBC Drivers?

If you are working with a 64-bit OS, you will encounter a problem which was not typically faced in the days of 32-bit OSes--namely, that you will have a separate set of 64-bit ODBC Drivers vs. a set of 32-bit ODBC Drivers.

So, when you type odbcad32.exe at the Run Command, you are ACTUALLY opening up the 64-bit ODBC Drivers that have been installed on the machine and NOT the 32-bit ODBC Drivers.

The 32-bit drivers can actually be found by typing the following path at the Run Command: \windows\SysWOW64\odbcad32.exe.  Therefore, if you are looking for the 32-bit Oracle drivers, you will only find these ODBC Drivers available in the 32-bit ODBC Drivers area.

When you open the ODBC Dialogs as well, you should look closely at the Title of the Dialog as well:

When you open the default ODBC Dialog, you will see a title of "ODBC Data Source Administrator (64-bit)".

However, when you open the 32-bit ODBC Dialog, you will see the following title instead: "ODBC Data Source Administrator (32-bit)"

If the titles match up, then you know you are in the correct place!!

Installing Oracle Client 12c on Windows 10

If you need to work with Oracle on your development machine, at some point in time, you are going to need to install the Oracle client on your machine.

Fortunately, if you install newer versions of Oracle clients, they will usually remain backwards-compatible with older versions of Oracle server.  For example, you can STILL install and Oracle 12c client while working with an Oracle 11g R2 database!

Without further ado, below you can find the steps to install Oracle Client 12c on your Windows 10 system:

For whatever reason, Oracle does not ship with necessary Visual C++ Prerequisites with its installer, so you will have to also separately install the Visual C++ Redistributable.

Otherwise, you may end up with errors such as the following when attempting to set up your ODBC DSNs for Oracle:

Based on the Oracle Pre-installation requirements, you can install a number of Visual C++ installers on your system ranging from Visual C++ 2010, to 2012 to Visual C++ 2013:

However, in my experience, the ONLY version of Visual C++ I was able to get to work on my machine was Visual C++ 2010 which you can download from here:

NOTE: If you are installing the 32-bit Oracle Client, then you will need to install the Visual C++ x86 installation and likewise if you are installing the 64-bit Oracle Client, you will need to install the Visual C++ x64 installation.

Sunday, September 25, 2016

Importing an Oracle Dump (.dmp) file for Oracle 11g R2

I will be the first one to admit that I am not an Oracle guru by any means since I primarily stick to the Microsoft stack (aka Microsoft SQL Server) so developing with Oracle for any project is always a bit of a struggle and a learning curve for me.

One of the common tasks that nearly every Oracle developer will have to face at some point in time is how to restore an Oracle Dump (.dmp) file!

Unfortunately, the documentation on just how to accomplish this is relatively sparse and doing this within Oracle is not as straightforward as restoring a backup into SQL Server.

After quite a bit of searching on the Internet and searching through various forums such as Stack Overflow, I was able to come up with a workable solution.

First of all, in order to get everything prepped for the Oracle import, you will have to set up a user for the import.  To save you a few headaches, it is best to try and open the .dmp file in Notepad++ or a similar editor to figure out which user the .dmp file was exported from and create it accordingly.

This set of commands can be run in SQLPlus, SQL Developer, Toad or any other Oracle IDE you prefer:

Once you have that user in place, you will have to finally perform the import.  One of the caveats to doing this, however, is that you MUST DO THIS ON THE SERVER!!  Attempting to perform this import on a client machine will fail miserably and leave you scratching your head as to why you cannot get the import commands to work!

Therefore, once you get the .dmp file uploaded to the server and establish an RDP session into your server, you can run the following command at the Windows command prompt.  (I tried getting it to run outside of the Windows command prompt such as in SQL Developer or Toad, but I could never get these IDEs to recognize the command):

That should be all that is necessary to get your .dmp file onto your Oracle 11g R2 instance!

Installing Oracle Database server 11g R2 on Windows

If you need to develop against an Oracle Database backend, you will probably want to be able to setup your own Oracle Database for development and testing.

Even though Oracle Database 12c has been out for quite some time, there will probably be numerous occasions where you have to install the older version of Oracle Database 11g R2 instead.  To do so, you simply have to download the Oracle Database 11g downloads from here (, extract the .zip files and then run setup.exe:

Saturday, September 24, 2016

Antivirus for Windows Server

If you are looking for Antivirus for Windows Servers, this can definitely be hard to find since most antivirus solutions are targeted towards Windows Desktop Operating Systems such as Windows 8 and Windows 10.

Well, thankfully, there are quite a few solutions available for Windows Server platforms such as Windows Server 2012 and Windows Server 2012 R2:

Friday, September 23, 2016

Mocking TempData in ASP.NET MVC Unit Tests

If you want to mock TempData in your ASP.NET MVC Unit Tests, you will encounter some difficulties with accomplishing this since TempData is based on the Controller context and therefore cannot be readily mocked.

If you start doing some digging for solutions on how to accomplish this, you will inevitable come across this article:

Of course, this works just fine if you only want to deal with TempData, but what if you find yourself needing to mock other objects that are dependent on the Controller context as well such as Session State?

Well, this is where MvcContrib TestHelper comes to the rescue!

Using just a few lines of code, this NuGet package will help you set up all of your MVC Controller dependencies such as TempData as well as Session State and several others!

Unfortunately, when you attempt to install this NuGet package into your Unit Testing project, you will end up seeing some unwanted dependencies (including an older version of ASP.NET MVC):

Fortunately, another clever developer has updated this library to support ASP.NET MVC 4 and later versions!

The name of this NuGet package is StudioDonder.MvcContrib.Mvc4.TestHelper.  You can download this NuGet package from here:

Even though it was originally built for ASP.NET MVC 4, it works just fine with ASP.NET MVC 5 as well:

After installing this NuGet package, you can use it just as if it was the original MvcContrib.TestHelper NuGet package in your code as follows: