Tag: General
All the articles with the tag "General".
-
Using the ASP.NET MVC ModelBinder attribute - Second part
Just after the ASP.NET MVC preview 5 was released, I made a quick attempt to using the ModelBinder attribute. In short, a ModelBinder allows you to use complex objects as action method parameters, instead of just basic types like strings and integers. While my aproach was correct, it did not really cover the whole picture. So here it is: the full picture. First of all, what are these model binders all about? By default, an action method would look like this: [code:c#] public ActionResult Edit(int personId) { // ... fetch Person and do stuff } [/code] Now wouldn't it be nice to pass this Person object completely as a parameter, rather than obliging the controller's action method to process an id? Think of this: [code:c#]
-
Forms interaction with ASP.NET MVC (screencast)
Abstract: "This screencast is a short demonstration on how you can handle form interactions using the ASP.NET MVC framework." Download sample code: MvcCommentForm.zip (593.58 kb)
-
ASP.NET MVC preview 5's AntiForgeryToken helper method and attribute
The new ASP.NET MVC preview 5 featured a number of new HtmlHelper methods. One of these methods is the HtmlHelper.AntiForgeryToken. When you place <%=Html.AntiForgeryToken()%> on your view, this will be rendered similar to the following: [code:c#] <input name="__MVC_AntiForgeryToken" type="hidden" value="Ak8uFC1MQcl2DXfJyOM4DDL0zvqc93fTJd+tYxaBN6aIGvwOzL8MA6TDWTj1rRTq" /> [/code] When using this in conjunction with the action filter attribute [ValidateAntiForgeryToken], each round trip to the server will be validated based on this token. [code:c#] [ValidateAntiForgeryToken] public ActionResult Update(int? id, string name, string email) { // ... } [/code]
-
Using the ASP.NET MVC ModelBinder attribute
ASP.NET MVC action methods can be developed using regular method parameters. In earlier versions of the ASP.NET MVC framework, these parameters were all simple types like integers, strings, booleans, … When required, a method parameter can be a complex type like a Contact with Name, Email and Message properties. It is, however, required to add a ModelBinder attribute in this case. Here’s how a controller action method could look like: [code:c#] public ActionResult Contact([ModelBinder(typeof(ContactBinder))]Contact contact) { // Add data to view ViewData["name"] = contact.Name; ViewData["email"] = contact.Email; ViewData["message"] = contact.Message; ViewData["title"] = "Succes!"; // Done! return View(); } [/code]
-
Form validation with ASP.NET MVC preview 5
In earlier ASP.NET MVC previews, form validation was something that should be implemented "by hand". Since the new ASP.NET MVC preview 5, form validation has become more handy. Let me show you how you can add validation in such a ridiculously easy manner. Here's an example controller: [code:c#] using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; namespace ValidationExample.Controllers { [HandleError] public class HomeController : Controller { // ... some other action methods ... [AcceptVerbs("GET")] public ActionResult Contact() { return View(); }
-
Building an ASP.NET MVC sitemap provider with security trimming
Warning!A new version of the source code provided in this post is available here. Use this blog post as reference only. Yes, it has been a while since my last post. A nice vacation to Austria, some work to catch up, ... All excuses, I know, but I'll make it up to you with a huge blog post! If you have been using the ASP.NET MVC framework, you possibly have been searching for something like the classic ASP.NET sitemap. After you've played with it, you even found it useful! But not really flexible and easy to map to routes and controllers. Sounds familiar? Continue reading! Doesn't ring a bell? Well, continue reading, please! Feel free to download the sample code. UPDATE: A version for preview 5 can also be downloaded: MvcSitemapProvider.cs (19.46 kb)
-
MSDN Chopsticks on ASP.NET MVC (screencasts)
A while ago, KatrienDG asked me to do some screencasts on the ASP.NET MVC framework for the MSDN Chopsticks page. I've been working on 2 screencasts: an introductory talk to the ASP.NET MVC framework and a Test Driven Development story. Feel free to leave some comments! Abstract: "The ASP.NET MVC framework is a new approach to web development, based on the model-view-controller design pattern. Microsoft built this framework on top of ASP.NET to allow this alternative to work with existing features like membership caching, user controls... In this video, Maarten shows you some basics on the ASP.NET MVC framework like creating a new controller action and a view."
-
ASP.NET MVC - Upcoming preview 4 release
ScottGu just posted that there's an upcoming preview 4 release of the ASP.NET MVC framework. What I immediately noticed, is that there are actually some community concepts being integrated in the framework, yay! And what's even cooler: 2 of these new features are things that I've already contributed to the community (the fact that it these are included in the MVC framework now could be coincidence, though...). Thank you, ASP.NET MVC team! This preview 4 release seems like a great step in the evolution of the ASP.NET MVC framework. Thumbs up!
-
Article on ASP.NET MVC in .NET magazine #21
Yesterday, I received the new Dutch edition of .NET magazine containing my article on the ASP.NET MVC framework. Since the article was written quite a while ago, soucre code is no longer up-to-date. Readers who are interested (or anyone else interested in ASP.NET MVC) can download up-to-date code examples on the ASP.NET MVC guestbook page.
-
Extending ASP.NET MVC OutputCache ActionFilterAttribute - Adding substitution
In my previous blog post on ASP.NET MVC OutputCache, not all aspects of "classic" ASP.NET output caching were covered. For instance, substitution of cached pages. Allow me to explain... When using output caching you might want to have everything cached, except, for example, a user's login name or a time stamp. When caching a full HTTP response, it is not really possible to inject dynamic data. ASP.NET introduced the Substitution control, which allows parts of a cached response to be dynamic. The contents of the Substitution control are dynamically injected after retrieving cached data, by calling a certain static method which returns string data. Now let's build this into my OutputCache ActionFilterAttribute...