web 2.0

Unit Testing Secure Controller Actions with Moq

One of the hardest things to unit test in MVC is security. Security is tough to test because there is a lot of setup involved in mocking the HttpContext, the Principal and the Identity. For example, in WeBlog I am using the following code in the Edit Post action. Post post = Repository.FirstOrDefault<Post>(x => x.ID == id); if (post == null) return View("NotFound"); if (!HttpContext.User.CanEditPost(post)) return View("PermissionDenied"); In order to make sure this code works properly I need to test it with an authorized and unauthorized user. Unfortunately, the HttpContext.User will not automatically be created for your tests so you have to mock one for each test that your perform. So lets start this journey by reviewing the code required to mock the HttpContext using the popular opensource library Moq. This code is a combination of code I discovered on Stackoverflow and Scott Hanselman’s MvcMockHelpers: MockContext public static Mock<Htt... [More]

Custom Configuration Sections FTW

As a .NET developer you will probably create hundred of configuration files over the course of your career. Most of the time when we use a configuration file it is for simple things like a database connection string. However, sometimes configuration data needs to be relational and can require a more complex structure than traditional name value pairs. For example, lets say that I am developing a data import tool which uses the configuration file to store data mapping information. Here is an example of what my configuration file might look  like: <import> <jobs> <job name="Foo"> <fieldMappings> <mapping source="column1" destination="TimeStamp"/> <mapping source="column2" destination="Name"/> </fieldMappings> </job> <job name="Bar"> <fieldMappings> <... [More]

How to Localize an ASP.NET MVC Application

While working on WeBlog this week I decided that I needed to start thinking about localization. If you have never heard the term “localization” before then its just a fancy way of saying that I want my application to be multi-lingual. In order to localize an application in .NET, you generally need to create a separate resource files for each language you want to support. In an ASP.NET MVC application, the resource files should be placed in a folder called App_GlobalResources. The folder can be created by right clicking on your project and selecting Add –> Add ASP.NET Folder –> App_GlobalResources. The resource files follow a naming convention. The first part of the name is the user defined part, for WeBlog we called it “Strings” but it could be whatever you want. The second part of the string is the Culture. For English the culture is “en”, for French the culture is “fr”, and so on and so forth. Here are few examples: Prefix Language Culture ... [More]

Advice for Aspiring ASP.NET Web Developers

For some reason, I have been asked the same question a lot lately. Which is, "I am new to development. Do you have any tips/tricks/suggestions to get me up to date on ASP.NET?". My knee jerk response to this question is usually to point people to Google for the answers. However, there is so much information floating around on the web these days that it can be overwhelming when you try to comb through it all. So, if you are an aspiring ASP.NET web developer then I hope you will find the following links, tips and tricks useful. The Basics of Web Development No matter what web technology you develop with, there are some basics that you should understand first: HTML – If you have never had exposure to HTML then you are going to want to visit W3 schools tutorial page before going any further. In its most primitive form, HTML is very simple and easy to use. Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation semantics (that is, the look and form... [More]

Building an RSS Feed in ASP.NET MVC

When building a website it is common to expose an RSS/ATOM feed for your content. Feeds serve two main purposes. The first, is that it allows other sites to consume your content for syndication. For example if you write .NET articles, there may be other partner sites that subscribe to your feed and dynamically pull in your content.  Secondly, feeds allow users to subscribe to your site so they can get notifications when your content is updated. This is especially relevant for sites with irregular content updates such as a personal blog. In the .NET 3.5 framework there is a namespace titled System.ServiceModel.Syndication which very few people seem to know about. The classes contained in this library allows you to create and consume RSS feeds with minimal effort. I recently created a feed for my WeBlog application. I was astonished about how little time it took to implement an RSS Feed. Instead of weighing you down with all the details...I'll let the code do the talking: public ... [More]