Artem Community
Artem interactive communities
ASP.NET MVC View - More then just a page view. Part II

Generate GSitemap View

Introduction

During my work on an ASP.NET MVC website I realized ASP.NET MVC views are more than just a page views and can be used to produce any kind of output.  
In the previous part of this series I gave an example how RSS output can be produces using a ASP.NET MVC controller and view.
This time I will show how simple is to generate  an output in Google sitemap (GSitemap) format and provide a dynamic sitemaps for Google crawler.

Code

Having an ASP.NET MVC web site I came to a task to generate some dynamic Google sitemap for helping the Google crawler to discover easily the dynamic content.

Here the code of my GSitemap generating:

  1. Part of my Controller class where I provide the data.

    /// <summary>

    /// Gets the products Google sitemap.

    /// </summary>

    /// <returns></returns>

    public virtual ActionResult GSitemap() {

        return View(Product.GetAll());

    }

  2. The view markup. Take a note there is no space between the page definition (<%@ Page ... %>) and the XML markup.

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Artem.WebSite.Models.Product>>" %><?xml version="1.0" encoding="utf-8" ?>

    <urlset xmlns="http://www.google.com/schemas/sitemap/0.84">

        <% foreach (var product in Model) { %>

        <url>

            <loc>http://artembg.com<%= product.DetailsUrl %></loc>

        </url>

        <% } %>

    </urlset>

Points of Interest

Once realizing the freedom We, ASP.NET developers, got with ASP.NET MVC Views, there is no limit of the views usage.

Happy coding ...


Posted 03-28-2009 9:02 by velio
Powered by Community Server (Non-Commercial Edition), by Telligent Systems