Skip to main content

Posts

Showing posts with the label C#

How to set Swagger as the default start page for API hosted on the Azure web app?

I created an Asp.Net Core 2.x Web API and configured Swagger on it, below is the code added in Configure method under Startup.cs file, for full swagger configuration, check here //Add swagger configuration app.UseSwagger(); app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" ); }); On my local machine when I run the API it is automatically redirected to the Swagger page. However, when I hosted this API as an Azure web app it is not redirecting directly to the Swagger and to access the swagger, I had to append /swagger in the URL, for example, https://testapi.azurewebsites.net/swagger/ Solution: Set RoutePrefix to string.Empty under app.UseSwaggerUI like below: app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" );      c.RoutePrefix = string .Empty; // Set Swagger UI at apps root }); And that’s it, now when you b...

C#: Geocoding to get Latitude and Longitude using Google Maps API

Geocoding is the process of getting the latitude and longitude of an address or set of addresses, which you can use to place markers or position the map. Geocoding can be done on the client-side as well as on the server-side, based on your requirements you need to choose one out of both options. For geocoding I am using Google Maps API which is quite reliable and faster in response to other geocoding APIs. You can check the JSON response by directly running the API with the address on the browser window or you can take service of POSTMAN as well. Sample JSON response of Google Map API is given below: Requested geocoding of "Gurgaon" using Google Maps API Request: http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=Gurgaon Response: {     "results" : [        {            "address_components" : [          ...

Read and parse a CSV file into an array of rows and columns in C#

The following PopulateCsvIntoArray method used to read the CSV file into a two-dimensional array of strings. I have included explanation of method lines, wherever needed. /// <summary> /// Populate the CSV file into an array, /// We assume that every line has the same number of fields and there may be blank lines. /// </summary> /// <returns></returns> private string [,] PopulateCsvIntoArray() {     // Get path of CSV file.     var path = Server.MapPath( "~/Folder_Name/testfile.csv" );     // Get the file's text using ReadAllText method.     string fileData = System.IO. File .ReadAllText(path);     // Split CSV data into lines.     fileData = fileData.Replace( '\n' , '\r' );     string [] lines = fileData.Split( new char [] { '\r' },         StringSplitOptio...

C#: Auto-Property Initializer in C# 6.0

Any developer who has worked with C# must have used properties sometimes during development. As you know, Auto-Property is declared with simple get and set (i.e. without any backing field), and can be initialized in the constructor once they are declared. In C# 6.0 a new features is introduced names as Auto-Property initializer . Auto-Property initializer allows property to be initialized like any other field in the same line where it has been declared. Let’s see Auto-Property Initializer in action: public bool UserName { get ; set ; } = "Sandeep" ; Auto-Property initializer in C# 6.0 also allows us to initialize read only Auto-Property in the same line where it has been declared. In older version of C#, we had to use a private set for read only properties, but with C# 6.0, without a private set, you can declare and initialize a property in the same line. Auto-Property Initializer for read only properties: public bool UserN...

Using LINQ concatenate unique items of two List and Sort them.

In this post I’ll show how you can combine unique items of two List < string > and then Sort them with the help of LINQ. Let’s take two List < string > contains employee names: List < string > empNameList1 = new List < string >() {     "Sandeep" ,     "Ashwani" ,     "Ashish" ,     "Saurav" }; List < string > empNameList2 = new List < string >() {     "Rahul" ,     "Sachin" ,     "Sandeep" ,     "Yuvraj" }; As you can see one name is common in both employee lists, so let’s write LINQ statement ( using the Enumerable .Concat method ) to get unique names and then sort them and finally print all the sorted unique names by looping through it. // Concatenate Unique Names of two List<string> and then sort. var finalNameList = empNameLis...