March 18, 2010
You may or may not have heard that ASP.NET MVC 2 has been released not too long ago. There are quite a few changes that come with ASP.NET MVC 2. In this article, I’m going to talk about some of the attribute changes related to MVC 2 now available to us:
ChildActionOnly – This attribute is used to designate that an action method of a controller can only be used to inject content in the page, via Html.Action or Html.RenderAction method. This means that the action method decorated with this attribute cannot be requested in the browser as a URL (for instance, the HomeController has an Index method with this attribute cannot be requested by http://localhost/Home/Index; the MVC framework will prevent it).
HttpGet – Equivalent to AcceptVerbs(HttpVerbs.Get)
HttpPost – Equivalent to AcceptVerbs(HttpVerbs.Post)
HttpPut – Equivalent to AcceptVerbs(HttpVerbs.Put)
HttpDelete – Equivalent to AcceptVerbs(HttpVerbs.Delete)
RequireHttps – Previously in the futures assembly, this attribute signals that the request will need to be for HTTPS url’s only. If not an HTTPS request, a redirect occurs.
From what I can tell, these are some of the new ones that I’ve found; this list is not necessarily complete, and as I find more, I’ll post my findings later.
If you are unfamiliar with attributes, by adding these attributes (such as [RequireHttps]public ActionResult DoThis()) to a method or class (when applicable) signals that some action will run at a specific time in the lifecycle. For instance, here may be the process for attributes that run before or after the execution/result:
Attribute – Before Action Method Execution
Action Method Execution
Attribute – After Action Method Execution
Attribute – Before Result Execution
Attribute – After Result Execution
And so attributes provide an aspect-oriented programming (AOP) approach to programming that wasn’t there before in any ASP.NET frameworks (without a third-party tool or open-source component, of course). These aren’t the only type of attributes available; attributes for logging selections, for choosing which action method to execute, are also some valid options.