Tag: c#

C# Round Robin Resources

Author: | Categories: Programming No Comments
The Problem: You have scarce / limited resources and you want to use these in and efficient way in your Code. E.g. You can only create N objects of certain class because of its limitation in terms of Resources or Licenses, and you want to share those Objects across M Consumers. The Solution: One way […]

Pass Data to Partial View – ASP.NET MVC

Author: | Categories: Programming No Comments
There are many ways to pass Data to the Partial View. I’ll discuss here method which I use in my projects which is passing Strongly Typed Model to the View. View Models public class ParentViewModel { public Id { get; set; } ..... public ChildViewModel Child { get; set; } } public class ChildViewModel { […]

Navigation Menu using ViewModel ASP.NET MVC

Author: | Categories: Programming 6 Comments
There are many approaches to Generate Dynamic Navigation Menus in ASP.NET MVC Applications, in this post i will discuss one approach using ViewModels and Partial Views which I use in my Applications. First I’ll create a Hard coded Navigation then replace it with based on ViewModel. Lets say we want to create a menu like […]

Login with User Name instead of Email in ASP.NET MVC Identity

Author: | Categories: Programming 2 Comments
When an ASP.NET MVC Application is created with “Individual Accounts” authentication, it is set to use Email address to Register and Sign in instead of User Name, so in this Post I’ll show how to use User Name to Register and Login. Open AccountViewModels.cs in Models folder, add below code in LoginViewModel and RegisterViewModel [Required] […]

ASP.NET download big file using BinaryWrite

Author: | Categories: Programming No Comments
There are situation when you need to provide download option for a big file located somewhere on server or generated at runtime. Below function could be used to download files of any size. Sometimes downloading big file throws exception OutOfMemoryException showing “Insufficient memory to continue execution of the program”. So this function also handle this […]

C# Create Instance of a Class from string

Author: | Categories: Programming No Comments
In C# You can create instance of any class dynamically using Activator.CreateInstance method public object GetInstance(string FullyQualifiedNameOfClass) { Type t = Type.GetType(FullyQualifiedNameOfClass); return Activator.CreateInstance(t); } If FullyQualifiedNameOfClass is in another assembly then loop through all assemblies and find the Type. For that use below code: public object GetInstance(string FullyQualifiedNameOfClass) { Type type = Type.GetType(FullyQualifiedNameOfClass); if […]

Write text file without Read lock in C#

Author: | Categories: Programming No Comments
Suppose there is a situation in which a service is writing a log file and there is another application which needs to open this file for reading. By default a file which is written is exclusively open by writer. To overcome this and make Write and Read non-exclusive use the following code. Writer var writer […]

Log4net logging in asp.net MVC

Author: | Categories: Programming No Comments
Make changes in web.config file as follows <configuration> <configSections> ... <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <appSettings> ... <add key="log4net.Config" value="log4net.config"/> <add key="log4net.Config.Watch" value="True"/> </appSettings> <log4net> <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="D:/Logs/TTG SmartANPR REST Service/smartanprrest_%date{ddMMyyyy}.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date %level - %message%newline" […]

WebClient with Timeout Support

Author: | Categories: Programming No Comments
By default the C# WebClient class does not let you modify the default timeout value of 1 minute. This subclass exposes the Timeout property so you can change the default without having to use a HttpWebRequest. using System; using System.Net; namespace MyNamespace { public class WebClientEx : WebClient { public int Timeout { get; set; […]

HTTP Status Code in case of Exception

Author: | Categories: Programming No Comments
This is how to get HTTP Status code in case of an Exception during HttpWebRequest catch(WebException ex) { HttpWebResponse res = (HttpWebResponse) ex.Response; Console.WriteLine("Http status code: " + res.StatusCode); }