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

Posted by on Comments (0)

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.aspx

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

Posted by on Comments (0)

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.aspx

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.

My own experience on Ubuntu Linux

Posted by on Comments (0)

Linux LogoOne thing I really like all the time is exploring new stuff, especially in computer’s world. I believe it’s the nature of every programmer in the world. About one month ago, I decided to start working with a new OS except Microsoft Windows and Mac OS; so switched to a Linux OS. As far as I know, Ubuntu is the easiest Linux ever (Some people believe OpenSUSE is better) so I decided to install the latest version of it on my laptop which is HP G62-a22SE (As you may know from my previous posts).

Installation

Linux is a very cool in installation process just like Mac OS. You can have full experience of an OS before installation. It means that the OS boots up right from its CD.

Partitioning is Linux is completely different from Windows. If you’re working with Windows, you need to be careful when partitioning your drives because on mistake can erase everything! Those people who have worked with Mac OS are familiar with this kind of partitioning. For more information about Linux partitioning schema check this out: http://content.hccfl.edu/pollock/AUnix1/Partitioning.htm

Installation progress in Linux is so cool too. The coolest thing is that basic user questions like administrator’s name/password, location and time zone, language options, etc. will be asked while installation is running in background so you won’t waist anytime during installation progress.

Another cool thing is that your network adaptors are automatically installed in the installation and you’re connected! There’s an option in the installation that can be set for automatic update so your OS will be updated right after the installation! Ubuntu automatically connects to the internet and receive latest updates while the OS is installing on your machine.

It took about 10-20 minutes to clean-install Ubuntu depends on your computer’s configuration.

Interface

The interface of the Linux depends on the type of Linux you’re installing. Some of them don’t have special appearances and some of them have. For instance, Ubuntu is very gorgeous because it has designed for personal use but fedora has a very simple UI. The Ubuntu UI is very similar to Mac OS. It has an application bar on the bottom of the screen and menu bar on the top. Working with Ubuntu is very simpler than other versions of Linux. Everything has made easy in this OS.

Performance

Performance is one of the key factors in Linux based Operating Systems. They’re all fast and responsive and they use all of the CPU resources to do a task faster. Because of this, your computer’s CPU temperature is a little higher than Windows. In my opinion, this OS is not built for laptops because their CPUs are weak. My own HP G62 laptop is getting very hot when working with Ubuntu on it. I think, Windows doesn’t use all CPU resources and because of this, you computer’s CPU temp won’t get high.

Applications

Ubuntu Software CenterUbuntu applications’ ranges are not as wide as Windows or Mac apps but in these couple of years, Linux developers have done a great job developing apps. The number of Linux applications is much larger than before but still not enough for many of us.

There’s an application named “Wine” which is so cool! You can run many of Windows applications on Linux. They’re currently support lots of applications and frameworks. You can also checkout their application’s database to see if they support your specific application.

Recently, they’ve implemented a new application on Ubuntu name “Ubuntu Software Center”. In a sentence, it’s just like an Apple AppStore but for Ubuntu applications. You can purchase and download any application you need. Most of the applications there are free but some of them are not.

Drivers & Support

As you may know, Linux Operating Systems are free and open source so obviously you shouldn’t expect any support from it. Although, they have a support system and forums for solving users’ problems but there’s no grantee at all! If you’re going to use a Linux OS, you have to keep in mind that you’re on your own.

The next point you have to consider is the drivers. Linux has the minimum number of users around the world as you can see in the following chart:

Usage share of web client operating systems

So hardware companies pay less attention to it. For example, AMD hasn’t released any reliable driver for ATI graphic card. You can search the web to see how many people who’ve ATI graphic cards have issues working in Linux OS.

Conclusion

Linux is very powerful OS in the world of Operating Systems’ world, but it’s not suitable for anyone. In a sentence, Linux is an advanced Operating System. If you’re an IT professional, maybe it’s suitable for you. In my very own opinion, Linux is good for PC not laptop! Because laptops’ batteries are limited and they can’t support an OS that uses all resources! If you’re going to develop Linux-based applications, I recommend using Mac OS.

All in all, it’s a very very powerful OS at no cost! Don’t forget that Linux OS is free and a lot of smart people working on it.

Hope it helps.

Posted in: Other   Tags: