How to read WordPress feeds in a C# MVC .Net application

This is a very easy method you could use to read XML data from a feed and instantiate the object you need with that information. For the following example, I am using an MVC .Net Application code.

First you need to create the Model to store the posts information.

 public partial class BlogPost
 {
    public string Name { get; set; }
    public string URL { get; set; }
    public string Description { get; set; }
    public System.DateTime Date { get; set; }
 }

The following step is used to read the XML data, I have created a helper class with a static method.

public class BlogPostsLookUp
 {
    public static IEnumerable<BlogPost> GetBlogPosts()
    {
      XDocument feedXML = XDocument.Load("http://clarifycoder.com/feed/");

      var feeds = from feed in feedXML.Descendants("item")
            select new BlogPost
            {
              Name = feed.Element("title").Value,
              URL = feed.Element("link").Value,
              Date = DateTime.Parse(feed.Element("pubDate").Value),
              Description = feed.Element("description").Value,
             };

       return feeds;
    }
 }

Once you have the list of blog post you can use it in the View for displaying the information

<ol class="blog-home">
    @foreach (var blog in BlogPostsLookUp.GetBlogPosts())
     {
       <li>
           <a href="@blog.URL" target="_blank">@blog.Name <br /></a>
           <span style="color: #b8c4c4">
               posted on 
               <span style="color:#808080;font-style:italic">
                  @blog.Date.ToString("MMMM dd, yyyy")
               </span>
           </span>
      </li>
    }

</ol>

Static Content Caching with ASP.Net MVC

I have been researching ways to cache contents in a website that I am working on. Below details how you could cache static contents in a ASP.Net MVC project.

This method does caching for CSS, JavaScript and image files as well.

Here is what you need to do 

Include the below content in your root web.config file, section system.webServer. The below code caches content for 7 days. 

  <staticContent>
      <clientCache  cacheControlMode="UseMaxAge" 
cacheControlMaxAge="7.00:00:00" />
    </staticContent>


What is the difference between ViewData, ViewBag and TempData in MVC.Net?

The ViewData, ViewBag and TempData are the mechanism in Asp.Net MVC which allows data to be passed between controller, view or the next action. The ViewBag and ViewData have same usage and behave very similarly unlike the TempData which has its own functions. Below I have described how they are used in different context.

ViewData

ViewData is a dictionary object which helps the data to be sent from the controller to the view. The data is accessible via a string key and sometimes it requires typecasting for complex data type.

public ActionResult Home()
{
ViewData["Title"] = "I am a software developer";
return View();
}

and in the view you can easily access the data.
@ViewData["Title"]

ViewBag

ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0 and does not require type casting for complex objects.

public ActionResult Home()
{
ViewData.Title = "I am a software developer";
return View();
}

and in the view you can easily access the data.
@ViewData.Title

TempData

TempData is also a dictionary object which is stored in session. The object is stored in session with a string key and value. This mechanism pass data from one controller to another and also between one action to another action.

example:

public ActionResult Index()
{
TempData["Message"] = "I am a software developer";
return RedirectToAction("Home");
}

public ActionResult Home()
{
var message = TempData[“Message”];
ViewBag.Message = message;
return View();
}

How to create dynamic database connection string in c#, entity framework

I have been working on creating a ticketing system for a client and they wanted to have a replica of the system install at a two location, each with its own database. In addition to this they want to be able to communicate with hosted database at different locations and generate report based on the data.

So I have been researching different approaches to take and found out the below described approach is the easiest.

  1. Create a table in the database to store the connection string details, I have the following items in the table.
    • Connection Name
    • Server Name
    • Database Name
    • Requires User Details ?
    • Username
    • Password (this is not really good, its better to ask the user for the password when required)

2.  Create views for user to manage the connection strings.

    •  List, edit and create

3.  Using the connection strings in querying.

I have created a view for reports and it has a drop-down list to select the connection string name. When the connection string name is selected, I can look up the database for the details of connection string and create a database connection string dynamically.

Dynamic Connection String example
Dynamic Connection String example

Generating a barcode with ZXing.Net

1. Installing ZXing.Net library.

Using nuget package installer install ZXing.Net libarary. Once installed you should be able to see zxing and zxing.presentation references.

2. Using ZXing library in a c# class. 

string content = barcodeNumber;

IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.EAN_13,
Options= new ZXing.Common.EncodingOptions
{
Height=100,
Width =200,
Margin=2
}
};

Bitmap barcodeBitmap;
var barcode= writer.Write(content);
MemoryStream stream = new MemoryStream();
barcode.Save(stream, ImageFormat.Bmp);