Category: ASP.NET

Asp.net GridView example using C#

Below is a sample code snippet for Asp.net GridView example using C#. The code snippet shows how to populate the GridView with a List of Employees. Asp.net GridView example using C# ASP.NET WebForms <asp:GridView ID=”GridView1″ runat=”server”> </asp:GridView> C# protected void Page_Load(object sender, EventArgs e) { List<Employee> employees = new List<Employee>(); employees.Add(new Employee { Name = “Test1”, Designtaion = “Software Engineer” }); employees.Add(new Employee { Name…

How to accept HTML Form Data in a ASP.NET Web API Action Method ?

There are times when you might want to create an endpoint in ASP.NET WebAPI that can handle the HTML form data posted as x-www-form-urlencoded. One of the options is to use the model binding concept.   In this case , the model should have the names of the keys that are passed in the HTTP request. The MediaFormatters are used to extract the data from…

How to get the Relative Server Path in ASP.NET Web API ?

In ASP.NET MVC , we can use the Server.MapPath or Request.MapPath to get the relative path of the server for a file. For example , The fileName would be the absolute path of the file. How to get the Relative Server Path in ASP.NET Web API ? You can use the HostingEnvironment.MapPath defined in System.Web.Hosting to get the relative path of the file as shown…

Server.MapPath with usage of . , .. , ~ and / Characters in ASP.NET

When using Server.MapPath in ASP.NET, the developers can specify the characters as shown below. var path1 = Server.MapPath(“.”)var path2 = Server.MapPath(“..”)var path3 = Server.MapPath(“~”)var path4 = Server.MapPath(“/”) What is the difference between the above methods ? Assume that the website is installed in the path D:\WebApplications\Abundantcode and the root of the IIS is C:\Inetpub\wwwroot and assume that the current page is http://abundantcode.com/popularposts The path1 will…

How to read a key from the Web.Config file in ASP.NET app using C# ?

If you want to read the values based on the key from the web.config file in your ASP.NET application using C# , you can use the  WebConfigurationManager to do it. How to read a key from the Web.Config file in ASP.NET app using C# ? Assume that the configuration file contains the below values If you want to read the value of the key (“UserName”)…

How to Add Web API to existing ASP.NET Web Forms Project ?

In one of the previous posts , we saw how to add the WebAPI to the existing ASP.NET MVC project. Well , the process of adding the ASP.NET Web API to the existing ASP.NET Web Forms project is still the same. You can easily do that by installing it from NuGet. Open the Package Console Manager in Visual Studio 2015 and run the following command….

Could not load file or assembly … An attempt was made to load a program with an incorrect format (System.BadImageFormatException)

There are times when you might get the following error when running the application from Visual Studio or opening your application page in the browser. Could not load file or assembly … An attempt was made to load a program with an incorrect format (System.BadImageFormatException) I received this error recently when trying to deploy the application in the IIS and accessing the page from the…