Log exception to file in c# .net core application explained

In production environment when any bug gets reported at that time as developer wants to get the exception details of bug to resolve the issue on high priority.
In some case there might be situation where exception is not logged in table but developer wants to check the bug immediately. for that purpose we need to write the
exception details including stack trace of bug in file. To implement this easily i am going to demonstrate the implementation of custom logging in file as below.

First we need to construct the exception and logger method in custom logger class as shown below.

public class CustomLogger { public static List<string> ConstructExceptionDetails(Exception ex,string FunctionName) { return new List<string>() { $"{DateTime.Now} - {FunctionName}", $"Exception: {ex.Message}", $"StackTrace: {ex.StackTrace}", $"InnerException: {ex.InnerException?.…

Using Ng-grid in AngularJS Pages

It is a common requirement for any project to display data in a tabular format. The easiest way to do this is by using HTML tables, but soon this gets complex, you need a way to support sorting (single & multi columns), resizable columns, inline editing, filtering, pagination (client-side and server-side) and so on. There is a way to bind data to a HTML Table in AngularJs, but when it comes to sorting, paging, editing and dragging the columns, the grid choice is useful. ng-grid is Angular's native implementation of the Grid. There are many plugins from third-parties that can be integrated into AngularJS Pages, but ng-grid exists inside the AngularJs framework and is very rich in functionality and compatibility.

Let us create a sample ng-grid and understand how ng-grid works.

To use AngularJS, you have to include it in your page before the closing <body> tag. Google's CDN is recommended for a faster load time:


Dependency injection in MVC controller step by step

ASP.NET Core MVC controllers should request their dependencies explicitly via their constructors. In some instances, individual controller actions may require a service, and it may not make sense to request at the controller level. In this case, you can also choose to inject a service as a parameter on the action method.

With ASP.NET 5 MVC6, we get a feature called dependency injection baked right into the framework for us. 

Dependency injection is a technique that follows the Dependency Inversion Principle, allowing for applications to be composed of loosely coupled modules. ASP.NET Core has built-in support for dependency injection, which makes applications easier to test and maintain.

ASP.NET Core's built-in support for constructor-based dependency injection extends to MVC controllers. By simply adding a service type to your controller as a constructor parameter, ASP.NET Core will attempt to resolve that type using its built in service container. Services are typically, but not al…

Asp.Net MVC page life cycle quick overview

There are seven main steps that happen when you make a request to an MVC web applications.

Routing Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table. When application starts at first time, it registers one or more patterns to the Route Table to tell the routing system what to do with any requests that match these patterns. An application has only one Route Table and this is setup in the Global.asax file of the application.

MvcHandler The MvcHandler is responsible for initiating the real processing inside ASP.NET MVC. MVC handler implements IHttpHa…

All about tempdata in Asp.Net MVC

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary. TempData stays for a subsequent HTTP Request as opposed to other options (ViewBag and ViewData) those stay only for current request. So, TempData can be used to maintain data between controller actions as well as redirects.

Note: Just like ViewData, typecasting and null checks required for TempData also in order to avoid errors.

Let’s see how we can use TempData in a practical scenario to pass data from one controller action to another.

//Controller Action 1 (TemporaryEmployee)public ActionResult TemporaryEmployee() { Employee employee = new Employee { EmpID = "121", EmpFirstName = "Imran", EmpLastName = "Ghani" }; TempData["Employee"] = employee; return RedirectToAction("PermanentEmployee"); } //Co…

Remove Owin from MVC 5 Application and use custom forms authentication

Owin comes with 15 other dependencies when we add default "blank" mvc5 template in solution. Which some time is irrelevant to web application hence needs to be removed for adding custom authentication techniques in the web application. Remove the owin  from solution is tedious task but if we follow the below procedure then it can be easily removed from the solution.

Right click on your project and from menu click on Manage Nuget Packages.
on left side of Manage Nuget Packages window click on Installed Package
then on right side of window in search box type owin. 

Uninstall packages in order of: web.config file and remove these sections from   <runtime><assemblyBinding xmlns=…

System.Security.SecurityException: Request failed. issue on web server [resolved]

While hosting your website on shared hosting server you might get the System.Security.SecurityException: Request failed issue which will give you below stack trace on landing page of the website.

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

You can add below code to your Application web.config file to resolve it permanently
<system.web><customErrorsmode="Off"/><trustlevel="Full"/></system.web> This issue might occur in the case when server does not have higher version of .Net framework installed. su…

Cannot create type 'webservice' error solution

We create a web-service in development environment it working fine. But when we deploy the developed web service in production server many people get the Cannot create type 'some web-service  error. After finding the solution on the net i got one resolution which i am going to share now.
To get ride of this error we need to re-install the .net framework 2.0 in IIS. This is essential to run web service on server. Following is the step you need to follow to register the .net framework in IIS.
Open command promptThen type >CD C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -iAfter above instruction the .net framework will be register in IIS.Now run the web service again. it should be run normally.Hurray you did it

Disable button after click in

To prevent double submit a form by user we need to restrict the user to click the button again. The idea is that we will use JavaScript to disable the button once user clicks first time.

Add the following lines on .aspx page

<asp:Button ID="btnProcess" runat="server" onclick="btnProcess_Click" Text="Process" />
Add the following line on .aspx.cs page (Page_Load).

string strProcessScript = "this.value='Processing...';this.disabled=true;"; btnProcess.Attributes.Add("onclick", strProcessScript + ClientScript.GetPostBackEventReference(btnProcess, "").ToString());

This will disable Button, and then change the text of the button "Processing...", and then continue on with the server side event btnProcess_OnClick event handler.

Find IE version using javascript

Many Web designers use browser detection techniques to ensure that their sites display properly when viewed with specific browsers. Some browser detection techniques encounter problems when viewed with later versions of the browser they're tailored for. For example, many Web designers used Cascading Style Sheets (CSS) rules that relied on the implementation of CSS in Microsoft Internet Explorer 6 to detect that version of the browser. When Windows Internet Explorer 7 provided additional support for the Cascading Style Sheets, Level 2 (CSS2) standard, many of these rules (also known as "CSS hacks") failed to detect the new version of the browser. As a result, sites that relied on these rules no longer displayed as intended. When using browser detection techniques, use techniques that support current and future versions of the browser you're targeting.

The most common way to detect Internet Explorer is to use client-side scripting to parse the user-agent str…