How to create dynamic XML sitemaps in ASP.NET MVC

When developing web applications (especially websites), some rules have to be obeyed. One of those rules are Sitemaps. Sitemap is an essential to tell more about a website to search engines. According to sitemaps.org, sitemaps are an easy way for webmasters to inform search engines about pages on their sites:

Sitemaps are an easy way for webmasters to inform search engines about pages on their sites that are available for crawling. In its simplest form, a Sitemap is an XML file that lists URLs for a site along with additional metadata about each URL (when it was last updated, how often it usually changes, and how important it is, relative to other URLs in the site) so that search engines can more intelligently crawl the site.

Implement sitemaps in ASP.NET MVC is very easy and this post is going to cover it.

There are several ways to implement sitemaps in ASP.NET MVC. Since sitemaps are just XML files, they can be generated using XLINQ (LINQ to XML) technology.

The process of creating a sitemap in a website can be divided into the following steps:

  • Get a list of URLs which has to be included in the sitemap.
  • Create the sitemap XML in code.
  • Return the created XML to the user.

Note: You don’t really need to create an actual XML file. All of the process above can be done inside a controller action.

The first step is to get the list of objects we want to create the sitemap based on. In this post, I have a list of categories which have a unique URL based on its ID. So:

public ActionResult SiteMap() {
    var items = categoryService.SelectAll();
}

After that, it’s time to create the XML document. Sitemaps must have a specific format; so, they can recognized by search engines. The following is a valid XML sitemap:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.example.com/</loc>
      <lastmod>2005-01-01</lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.8</priority>
   </url>
</urlset>

As you know, creating such XML file in LINQ to XML is a very simple task to do:

public ActionResult SiteMap() {
    XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
    const string url = "http://example.com/Category/{0}";
    var items = categoryService.SelectAll();
    var sitemap = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XElement(ns + "urlset",
        from i in items
        select
        new XElement(ns + "url",
            new XElement(ns + "loc", string.Format(url, i.CategoryId)),
            new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
            new XElement(ns + "changefreq", "always"),
            new XElement(ns + "priority", "0.5")
    )));
}

As you can see in above code, we’ve simply created an XML file in sitemap format. Now we can return it to the user.

return Content("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + sitemap.ToString(), "text/xml");

Look at the code above. When creating normal actions, we usually return a View or PartialView objects; but, since we’re going to return a content, we use the Content method. Content method requires two arguments. The first argument is the content we’re going to return to the user as string and the second is the content’s MIME type.

Hope it helps.

The most essential apps I use everyday

Since I purchased my first ever smartphone which was an iPhone 3G, I started using some apps to help me get organized, learn more, stay in touch with my friends and family and have fun on my device.

During these four years I have used and tried a lot of apps and selected some of them to fulfill my needs. Nowadays, I can’t live without some of these apps. In this post I am going to share some of the most effective apps I use everyday. I thought this might be useful for you as well.

1. iPhone’s official Mail App

Mail appOne of the most important applications I use on my iPhone is the Mail app. The built-in iPhone mail app is not as feature-rich as some other third-party applications but I think its features are enough. It covers all of my expectations including the following:

  • It let me setup multiple accounts on it. Currently, I have setup my iCloud account and my main (i [at] ramezanpour [dot] net) account on it.
  • It supports signatures.
  • Its has a feature called VIP; so, you can add the most important people you’re communicating with in it. Email from those people will be shown bolder than others; also, they will be placed in a separate folder called VIP.
  • It supports all major email providers including GMail, Yahoo, AOL, Outlook and also Exchange.

2. Podcasts

Podcasts appOne of the iOS’s most unique features over Android or other mobile operating systems is the podcast. Podcasts app will let you have all of your favorite podcasts in one place. I usually connect my iPhone to my car’s stereo and listen to my favorite podcasts instead of listening to music.

Podcasts are available in a wide range of categories such as music, news, arts, education, science and technology. I have subscribed to some science and technology podcasts and listen to them when on my way to work or returning home. My wife also likes some of them; so we listen to them together.

3. Viber

ViberAlmost all of friends are using Viber nowadays. It’s the most popular messaging app in Iran. According to a report, more than 40 million Iranians are using Viber these days. Viber let me stay in touch with my friends and family at a very low cost. It’s not the best messaging app (I perfer Whatsapp messenger) but since most of my friends are using it’s difficult to find them on other messaging apps.

4. Skype

SkypeAlthough Viber covers all of my text needs; but, when it comes to voice and video messaging, Skype is by far the best option. The voice quality is awesome (Even with Iran’s bad internet service) and the service is almost free. I use Skype to talk to some of my friends who live overseas. Beside its mobile app, Skype is also available for desktop computers as I am currently using it in my Windows and Mac machines.

5. Evernote

Evernote“Getting used to productivity apps is always a very hard thing to do but once you get used to them, your life gets brighter.” – Mohammad M. Ramezanpour

Evernote is one of those apps I use almost everyday. Since I use Evernote I don’t use papers anymore. I use Evernote for taking notes and creating checklists. One of the good features of Evernote is the ability to share a note or notebook with someone else. My wife has created a shopping list in Evernote and shared it to me. Every time she needs something at home, she adds a new item to that list. We go shopping almost every week and purchase things according to that list.

6. DropBox

DropBoxDropBox is the best online cloud storage. I have been using SkyDrive (OneDrive) and Google Drive but in my opinion, non of them were as powerful as DropBox. DropBox is available in all operating systems. I use it in my iPhone, Windows and Mac devices. DropBox is very simple and easy to use. One of the coolest features of DropBox in comparison with other cloud storage apps is its “Lan Sync” feature which let users to share files in-between without using the internet bandwidth. all of the files will be transferred via local network.

I use some other apps but these 6 apps are very essential for me. What apps do use the most? Please share your list by commenting down below.

Hope it helps.

Create an on-demand VPN connection programmatically in iOS 8

A while ago, I published a post about configuring and managing VPN connections programmatically in iOS 8. By default, established VPN connection will be disconnected if user iOS device goes to sleep to save battery life. To avoid this, Apple introduced a feature called on-demand; so, iOS will get connected to VPN whenever it needs to connect to the internet.

Well, it can be implemented programmatically too using the NetworkExtension framework and that’s exactly what this post is all about. In this post, I am going to create an on-demand VPN connection using NetworkExtension; therefore, VPN connection will be established whenever an app opens a network connection.

Note: I am not going to describe how to create a VPN connection in this post. If you’re not familiar with creating a VPN connection programmatically, please take a checkout my post.

Turn on On-Demand

The first thing you need to do is to tell the NetworkExtension framework that you want to create an on-demand connection. To do so, set the onDemandEnabled property to YES:

[[NEVPNManager sharedManager] setOnDemandEnabled:YES];

turning on-demand on is not enough. You will also need to tell the OS when exactly you want on-demand to be enabled. To do so, you will need to assign some rules to your configuration. These rules called “On-demand rules”:

What are On-demand rules?

On-demand rules are set of attributes which must be set to tell the OS when VPN connection should be established on-demand. onDemandRules property accepts an array of rules. Consequently, you can set multiple rules for a VPN configuration.

For example, you can set a rule and tell the OS to establish the VPN connection whenever user wants to open Apple.com; otherwise, the VPN connection won’t be established.

One thing you may want to do it to activate the VPN connection whenever an app open a network connection; so, all iOS network traffic will be transferred through your VPN server. To achieve this, NEOnDemandRuleConnect class must be used.

In Network Extension framework, Apple has provided some useful on-demand rule templates you can make use of. Although you can create your own rule, it’s possible to use templates as well.  NEOnDemandRuleConnect class is one of those templates. It will tell the OS to establish VPN connection whenever iOS needs to connect to the internet; as a result, users will always connect to your VPN servers whenever they want to access the internet. As far as I know, this is what most VPN providers and users want:

[[NEVPNManager sharedManager] setOnDemandEnabled:YES];
NSMutableArray *rules = [[NSMutableArray alloc] init];
NEOnDemandRuleConnect *connectRule = [NEOnDemandRuleConnect new];
[rules addObject:connectRule];
[[NEVPNManager sharedManager] setOnDemandRules:array];

Once you changed the configuration you have to save it using saveToPreferencesWithCompletionHandler: method.

Hope it helps 🙂