Optional Parameters in Web API

Let us see how to make one or more parameters as optional in Web API.

[HttpGet]
[Route("Vehicles/GetDetails/{CompanyID}/{VehicleType}/{ModelNo}")]
public HttpResponseMessage GetDetails(String CompanyID, string VehicleType, string ModelNo)
{
    ...
}

Usually this method can be called like –

http://127.0.0.2:9966/Vehicles/GetDetails/101/SUV/DK999

If we want to make VehicleType as optional and pass it as null value like below, it will throw an error.

http://127.0.0.2:9966/Vehicles/GetDetails/101//DK999

 
This can be accomplished by moving all the optional parameters to last, assigning them a default value and passing them as separate values in the URL.
The method should be modified as –

[HttpGet]
[Route("Vehicles/GetDetails/{CompanyID}/{ModelNo}")]
public HttpResponseMessage GetDetails(String CompanyID, string ModelNo, string VehicleType = "ALL")
{
    ...
}

Then we can call the method as –

http://127.0.0.2:9966/Vehicles/GetDetails/101/DK999?VehicleType=SUV

We can also add it to body as well.

Changing the default language in Visual Studio

You might have came across a situation where you set the default version of Visual Studio to some language mistakenly or you want to change it because of some reason. Here is the solution for it.
It might be looking silly. But for those who are in need. It mean a lot, I suppose.
There are two ways of doing this.
First:

  1. Choose Tools -> Import and Export Settings…
  2. Select Reset All Settings and click Next
  3. Select whether you would like to save the current settings (I would say not here) and click Next
  4. Select the settings you want to use(change the language to C#) and click Finish

But, this is not advisable if there are any environmental settings that are set in the VS. In this case, you can use the following approach.
Second:
Modifying CurrentSettings.vssettings file. This file can be found in My DocumentsVisual Studio xSettings directory.
Open CurrentSettings.vssettings in a text or XML editor, and search for DefaultProjectLanguage.
You can then change the value accordingly. Here are the two options for VB or C#.

<PropertyValuename="DefaultProjectLanguage">VB</PropertyValue>

<PropertyValuename="DefaultProjectLanguage">CSharp</PropertyValue>

Save the file, and once you open Visual Studio, you’re set.
Hope this helps!

    	
  1. Choose Tools -> Import and Export Settings...
  2. Select Reset All Settings and click Next
  3. Select whether you would like to save the current settings (I would say not here) and click Next
  4. Select the settings you want to use(change the language to C#) and click Finish