Quick post: How to access an action in different areas in ASP.NET MVC

Sometimes I like to post about something, but that thing is very simple and can be described in one or two paragraphs. Previously, I preferred not to post these kinds of things as a blog post but recently, I decided to post almost anything in my blog no matter how many characters they are. So here you go!

When developing an ASP.NET MVC 3 application, you may want to partition your application into different areas for any kinds of reasons:

The MVC pattern separates the model (data) logic of an application from its presentation logic and business logic. In ASP.NET MVC, this logical separation is also implemented physically in the project structure, where controllers and views are kept in folders that use naming conventions to define relationships. This structure supports the needs of most Web applications.

However, some applications can have a large number of controllers, and each controller can be associated with several views. For these types of applications, the default ASP.NET MVC project structure can become unwieldy.

To accommodate large projects, ASP.NET MVC lets you partition Web applications into smaller units that are referred to as areas. Areas provide a way to separate a large MVC Web application into smaller functional groupings. An area is effectively an MVC structure inside an application. An application could contain several MVC structures (areas).

For more about MVC areas, check this out: http://msdn.microsoft.com/en-us/library/ee671793/

When want to develop a web application using MVC, you probably need to access actions/partial views from different areas. For example: You need to have an ActionLink to access the login action in Admin area. In order to do so, you simply need to add a RouteAttribute to your ActionLink as following:

@Html.Action("LoginStatus", "Users", new { area = "Admin" })

If you want to access root area, all you need to do is to set the area name to String.Empty:

@Html.Action("LoginStatus", "Users", new { area = "" })

Hope it helps.

Using SyndicationFeed class to create and read RSS/Atom feeds

Feeds are one of the most important elements in every website out there. One of the main ways to make your website’s contents shareable is to use these kinds of feeds. It also one of the feature I prefer to add in my entire website projects.

Creating and reading RSS/Atom feeds in .NET Framework have a lot of ways of implementations. Some folks are using XLINQ and some using XmlWriter or XmlReader. One of the most popular ways to create RSS feeds is the following:

protected void Page_Load(object sender, EventArgs e)
{
  // Clear any previous output from the buffer
  Response.Clear();
  Response.ContentType = "text/xml";
  XmlTextWriter feedWriter 
    = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

  feedWriter.WriteStartDocument();

  // These are RSS Tags
  feedWriter.WriteStartElement("rss");
  feedWriter.WriteAttributeString("version", "2.0");

  feedWriter.WriteStartElement("channel");
  feedWriter.WriteElementString("title", "Daily Coding");
  feedWriter.WriteElementString("link", "http://www.dailycoding.com");
  feedWriter.WriteElementString("description", "Daily Coding");
  feedWriter.WriteElementString("copyright", 
    "Copyright 2008 dailycoding.com. All rights reserved.");
 
  // Get list of 20 most recent posts
  PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

  // Write all Posts in the rss feed
  foreach(PostInfo post in posts)
  {
    feedWriter.WriteStartElement("item");
    feedWriter.WriteElementString("title", post.Title);
    feedWriter.WriteElementString("description", post.PostHtml);
    feedWriter.WriteElementString("link", 
      UrlHelper.GetShowPostUrl(this, post.Name));
    feedWriter.WriteElementString("pubDate",
      post.DatePosted.ToString());
    feedWriter.WriteEndElement();
  }

  // Close all open tags tags
  feedWriter.WriteEndElement();
  feedWriter.WriteEndElement();
  feedWriter.WriteEndDocument();  
  feedWriter.Flush();
  feedWriter.Close();

  Response.End();
}

Here’s the original post for the code snippet above: http://www.dailycoding.com/Posts/create_rss_feed_programatically_from_data_in_c/

But maybe we should change the way we create and read these feeds.

In .NET Framework 3.5, Microsoft introduced a new class named SyndicationFeed! This class is in System.ServiceModel namespace so you should add a reference to this assembly in order to make use of SyndicationFeed class.

SyndicationFeed Represents a top-level feed object, <feed> in Atom 1.0 and <rss> in RSS 2.0.

Using SyndicationFeed:

In order to get data from a RSS link, you just need to create an instance of SyndicationFeed class.

If you want to create a new feed to publish, just create a default instance of the class; otherwise, you need to use SyndicationFeed.Load static method to load data from a link:

When the SyndicationFeed object filled, you can make use of its properties and methods to get any kind of data you want from specified feed. Here’s an example of creating a list that represents last 5 RSS feed items in a page:

SyndicationFeed feedReader = 
SyndicationFeed.Load("http://ramezanpour.net/syndication.axd"); @foreach (var item in feedReader.GetFeed().Items.Take(5)) { <div> <strong>
<a href="@item.Links[0].Uri.OriginalString">@item.Title.Text</a></strong> <p>@Html.Raw(item.Summary.Text)</p> </div> }

Hope it helps.