web 2.0

Building Better MVC Code with T4MVC

If you are a ASP.NET web developer than chances are that you have heard of or dealt with problems related to “Magic Strings”. Magic strings are taboo because they introduce a degree of fragility in your code due to the fact that they are not strongly typed. The classic example, is referencing a View Name or Route in your controller action using a string value: public class HomeController : Controller { public ActionResult Index() { return RedirectToAction("Foo"); } public ActionResult Foo() { return View(); } } As you would expect, when the Index action is invoked the user gets redirected to “Foo”. Unfortunately, if I rename the “Foo” method to “Bar” my code will still compile but it will ultimately fail at runtime. This is all due to the fact that are RedirectToAction method is using the magic string “Foo” which is not strongly typed and therefore not caught by the compiler. As a workaround to this ... [More]

Error Handling in MVC with ELMAH

What is ELMAH? In case you have been living under a rock I will start this block post by giving you a basic introduction to ELMAH. If you already are familiar with ELMAH then just skip to the next section. The following description was taken verbatim from the ELMAH website… ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment. Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code: Logging of nearly all unhandled exceptions. A web page to remotely view the entire log of recoded exceptions. A web page to remotely view the full details of any one logged exception. In many cases, you can review the original yellow screen of deat... [More]

How to Build a Custom View Engine with Theme Support

All good blogging platforms have theme support. So while working on WeBlog I initially implemented theme support by using a base controller class. The base controller class was responsible for dynamically setting the master page at runtime. I did this by assigning the action’s MasterName property in the OnActionExecuted event. Here is a short snippet of code which outlines the process. public class BaseController { protected override void OnActionExecuted(ActionExecutedContext filterContext) { var action = filterContext.Result as ViewResult; if (action != null) { action.MasterName = MyApp.Properties.Settings.Default.Theme; } base.OnActionExecuted(filterContext); } } Although the BaseController concept worked, I never liked that fact that all my other controllers had to inherit from it. As a matter of fact, when I added the BaseController class to my project I made myself an action item to research Custom View E... [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]

Display and Editor Templates in ASP.NET MVC 2

In the first version of ASP.NET MVC, I found myself creating a lot of HTML Helpers and Partial Views in order to promote code re-use and to standardize formatting of certain data types across views. For example, let's say that I want every date in my application to use the formatting of “MM/dd/yyyy”. In MVC 1.0, I would have made the following HTML Helper: public static string FormatDate(this HtmlHelper helper, DateTime value ) { return value.ToString( "MM/dd/yyyy" ); } Once the HTML Helper was ready, I would add the markup to my views: <%= Html.FormatDate( Model.Date ) %> Although this code is reusable it can be hard to maintain when you have a large application. It is especially painful to implement when you already have dozens of views and you need to replace the old "HTML.Encode( Model.Date )" code with the new helper method. Fortunately, there is a better way! MVC 2.0 introduced the concept of Display and Editor Templates. First of all... [More]

Tags: , ,