<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>LA.NET [EN] : MVC</title><link>http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx</link><description>Tags: MVC</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>SerialSeb’s MVC best practices</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/05/08/serialseb-s-mvc-best-practices.aspx</link><pubDate>Fri, 08 May 2009 08:42:52 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1692063</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1692063</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/05/08/serialseb-s-mvc-best-practices.aspx#comments</comments><description>&lt;p&gt;I’ve just finished seeing &lt;a href="http://serialseb.blogspot.com/"&gt;Sebastien Lambla&lt;/a&gt;’s (aka &lt;a href="http://serialseb.blogspot.com/"&gt;SerialSeb&lt;/a&gt;) MVC best practices &lt;a href="http://exposureroom.com/members/irascian.aspx/assets/1fc0a77839354170a009e6bcaffa868c/"&gt;presentation&lt;/a&gt;. It’s cool and I’m recommending it (especially if you’re new – and, of course, if you have the time for it). I’m in favor of most of his recommendations (the only that I don’t really agree is the code-behind rule -&amp;#160; as I said, I understand that most people shouldn’t use it, but I still maintain everything I &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/09/19/codebehind-files-in-asp-net-mvc-are-not-evil.aspx"&gt;said&lt;/a&gt; about it in the past).&lt;/p&gt;  &lt;p&gt;So, if you have time, go &lt;a href="http://exposureroom.com/members/irascian.aspx/assets/1fc0a77839354170a009e6bcaffa868c/"&gt;watch it&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1692063" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>MVC Action filter project on Codeplex</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/28/mvc-action-filter-project-on-codeplex.aspx</link><pubDate>Tue, 28 Apr 2009 09:46:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1691458</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1691458</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/28/mvc-action-filter-project-on-codeplex.aspx#comments</comments><description>&lt;p&gt;Roni Schuetz informed me of a new &lt;a href="http://www.codeplex.com"&gt;Codeplex&lt;/a&gt; project he created for building a &lt;a href="http://mvcactionfilter.codeplex.com/"&gt;common library&lt;/a&gt; of ASP.NET MVC action filters. If you’re into MVC and you’d like to contribute, then &lt;a href="http://mvcactionfilter.codeplex.com/"&gt;check it out&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1691458" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the ActionLink control</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/13/the-mvc-framework-the-actionlink-control.aspx</link><pubDate>Mon, 13 Apr 2009 20:43:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1687281</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1687281</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/13/the-mvc-framework-the-actionlink-control.aspx#comments</comments><description>&lt;p&gt;Ok, so today I’ve finally updated my local copy of the MVC source code (which I’m doing through my preferred SVN client) and lo-and-behold: new stuff to talk about in the MVC futures source code! Today we’re going to talk about the ActionLink control. &lt;/p&gt;  &lt;p&gt;As you might expect (from the name), this is a really simple control which extends the base MvcControl. Currently, it exposes the following properties:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;ActionName: lets you pass the action name that should be used for rendering the link;&lt;/li&gt;    &lt;li&gt;ControllerName: lets you set the name of the controller. Used for influencing the generated url;&lt;/li&gt;    &lt;li&gt;RouteName: as you’ve probably guessed, you can use this to generate a link by using its name;&lt;/li&gt;    &lt;li&gt;Values: lets you pass a dictionary for setting the route’s dictionary values;&lt;/li&gt;    &lt;li&gt;Text: used for setting the text that is generated for the link.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt; If you’re used to using the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/01/29/the-mvc-framework-working-with-anchors.aspx"&gt;Html.ActionLink helper&lt;/a&gt;, then there really shouldn’t be anything new in here. Anyway, here’s an example of how you can use this new control in your page:&lt;/p&gt;  &lt;p&gt;&amp;lt;mvc:actionlink runat=&amp;quot;server&amp;quot;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ActionName=&amp;quot;About&amp;quot;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Text=&amp;quot;Go to about&amp;quot; /&amp;gt;&lt;/p&gt;  &lt;p&gt;Unlike the other controls, the name property isn’t really used internally to get any info from the view data dictionary. You’re probably wondering how you can fill the Values property. Nothing better than a quick example…the next snippet creates a link to the Index action and sets its id property (I’m using the default routes you get when you create a new MVC app):&lt;/p&gt;  &lt;p&gt;&amp;lt;mvc:actionlink runat=&amp;quot;server&amp;quot; ActionName=&amp;quot;Index&amp;quot; Text=&amp;quot;Go to about&amp;quot; name=&amp;quot;message&amp;quot;&amp;gt;   &lt;br /&gt;&amp;#160; &amp;lt;Values id=&amp;quot;1&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;lt;/mvc:actionlink&amp;gt;&lt;/p&gt;  &lt;p&gt;This ends up generating the following HTML:&lt;/p&gt;  &lt;p&gt;&amp;lt;a name=&amp;quot;message&amp;quot; href=&amp;quot;/MvcRTM/home/Index/1&amp;quot;&amp;gt;Go to about&amp;lt;/a&amp;gt;&lt;/p&gt;  &lt;p&gt;If you had more parameters, you could simply add them as attributes to the Value element. And that’s it for today. Stay tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1687281" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the AcceptAjaxAttribute</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/09/the-mvc-framework-the-acceptajaxattribute.aspx</link><pubDate>Wed, 08 Apr 2009 23:01:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1686060</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1686060</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/09/the-mvc-framework-the-acceptajaxattribute.aspx#comments</comments><description>&lt;p&gt;There might be some scenarios where you have methods that should only be invoked for AJAX requests. In those cases, you can use the AcceptAjaxAttribute to decorate those methods. This is an ActionMethodSelectorAttribute which overrides the IsValidForRequest and delegates all its work to the Request.IsAjaxRequest extension method. &lt;/p&gt;  &lt;p&gt;When you’re not in an AJAX request, the IsAjaxRequest method returns false and you end up getting a 404 for that call. It’s really a simple attribute and you might want to &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/02/04/the-mvc-platform-interacting-with-the-actionmethodselectorattributes.aspx"&gt;recall&lt;/a&gt; how ActionMethodSelectorAttributes interact with the remaining elements of the platform.&lt;/p&gt;  &lt;p&gt;And that’s all there is to say about this attribute. Stay tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1686060" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: setting a timeout</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/07/the-mvc-framework-setting-a-timeout.aspx</link><pubDate>Tue, 07 Apr 2009 20:58:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1685678</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1685678</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/07/the-mvc-framework-setting-a-timeout.aspx#comments</comments><description>&lt;p&gt;In the last posts, we’ve taken a look at how we can add asynchronous action to a controller. Today, we’re going to dig a little deeper and see how we can specify a timeout that limits the maximum amount of time that the asynchronous operations can take to complete.&lt;/p&gt;  &lt;p&gt;By default, you inherit a default timeout: all actions must complete under 30 seconds (as you can see, if you peek into the AsyncManager class). You can specify the timeout in two ways: by interacting directly with the AsyncManager’s Timeout property or by using one of the existing timeout attributes.&lt;/p&gt;  &lt;p&gt;If you want to use an attribute, then you can simply decorate a method (or a controller class) with the AsyncTimeoutAttribute. This is an action filter attribute which receives an integer that defines the valid timeout for the current asynchronous request. If you don’t want to limit the asynchronously execution in time, then you can simply pass Timeout.Infinite(–1) to the constructor. Alternatively, you can also annotate the method/controller with the NoAsyncTimeoutAttribute (this attribute expands the previous one and passes Timeout.Infinite to its constructor).&lt;/p&gt;  &lt;p&gt;And that’s all for today. Keep tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1685678" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the RequireSslAttribute</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/06/the-mvc-framework-the-requiresslattribute.aspx</link><pubDate>Mon, 06 Apr 2009 21:00:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1685298</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1685298</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/06/the-mvc-framework-the-requiresslattribute.aspx#comments</comments><description>&lt;p&gt;The RequireSslAttribute is a filter attribute which you can apply to a class or method and which is responsible for overriding the OnAuthorization method and seeing if the current request is being done over SSL. The filter exposes a single property, called Redirect. &lt;/p&gt;  &lt;p&gt;When it detects that the current request is not under SSL, it checks the Redirect property: if it’s set to true, then it will build an https url based on the current request and redirect the user to that new url; if the Redirect property is set to false, then it will simply throw an exception.&lt;/p&gt;  &lt;p&gt;And that’s all there is to know about this attribute. Keep tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1685298" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the “delegate” pattern for asynchronous actions</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-delegate-pattern-for-asynchronous-actions.aspx</link><pubDate>Sun, 05 Apr 2009 21:13:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684957</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684957</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-delegate-pattern-for-asynchronous-actions.aspx#comments</comments><description>&lt;p&gt;This is the last post in the asynchronous action pattern series. In this post, we’re going to present the (so called) “delegate” pattern. This is really similar to the “event” pattern. The difference is that, in this case, we return a parameterless delegate from our begin method which will be called when all the asynchronous operations end. &lt;/p&gt;  &lt;p&gt;To illustrate this pattern, we’re going to update the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-simplifying-the-event-pattern.aspx"&gt;previous example&lt;/a&gt;. Here’s the final code:&lt;/p&gt;  &lt;p&gt;public Func&amp;lt;ActionResult&amp;gt; GetHtml(String url) {   &lt;br /&gt;&amp;#160; var req = WebRequest.Create(url);    &lt;br /&gt;&amp;#160; req.Method = &amp;quot;GET&amp;quot;;    &lt;br /&gt;&amp;#160; AsyncManager.RegisterTask(cb =&amp;gt; req.BeginGetResponse(cb, null),    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ar =&amp;gt; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var response = req.EndGetResponse(ar);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (var reader =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new StreamReader(response.GetResponseStream())) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ViewData[&amp;quot;contents&amp;quot;] = reader.ReadToEnd();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ); &lt;/p&gt;  &lt;p&gt; return () =&amp;gt; View(“Index”);   &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;When compared with the “event” pattern, you can quickly see that:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;the begin method returns a parameterless delegate which will be called when all the asynchronous operations end;&lt;/li&gt;    &lt;li&gt;since we can’t pass any parameters to our custom callback method, we write the HTML directly to the ViewData dictionary;&lt;/li&gt;    &lt;li&gt;we use a Lambda expression since the view we’re rendering doesn’t have the same name as the action method (if that was the case, we could simply return a reference to the View method, ie, we could simply write return View;).&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And that’s all for today. More about the MVC framework on the next posts. Keep tuned!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684957" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: simplifying the “event” pattern</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-simplifying-the-event-pattern.aspx</link><pubDate>Sun, 05 Apr 2009 21:01:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684955</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684955</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-simplifying-the-event-pattern.aspx#comments</comments><description>&lt;p&gt;In the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-event-pattern.aspx"&gt;previous post&lt;/a&gt;, we’ve taken a look at the “event” pattern. In this post, we’re going to see how we can simplify the needed code for using this pattern. The AsyncManager exposes a RegisterTask method which simplifies the needed code when we decide to use this pattern. We’ll improve the previous example by using this method. Here’s the code:&lt;/p&gt;  &lt;p&gt;public void GetHtml(String url) {   &lt;br /&gt;&amp;#160; var req = WebRequest.Create(url);    &lt;br /&gt;&amp;#160; req.Method = &amp;quot;GET&amp;quot;;&amp;#160; &lt;br /&gt;&amp;#160; AsyncManager.RegisterTask(cb =&amp;gt; req.BeginGetResponse(cb, null),    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; ar =&amp;gt; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var response = req.EndGetResponse(ar);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (var reader =     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new StreamReader(response.GetResponseStream())) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AsyncManager.Parameters[&amp;quot;contents&amp;quot;] = reader.ReadToEnd();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ); &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;public ActionResult GetHtmlCompleted(String contents) {   &lt;br /&gt;&amp;#160; ViewData[&amp;quot;contents&amp;quot;] = contents;    &lt;br /&gt;&amp;#160; return View(&amp;quot;Index&amp;quot;);    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;As you can see, the code is really similar to the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-event-pattern.aspx"&gt;one&lt;/a&gt; we’ve shown in the other post. The main difference is that with this helper method you’ll only need to worry with setting up the asynchronous action.&lt;/p&gt;  &lt;p&gt;And that’s all for today. On the next post, we’ll talk about the latest option for having asynchronous actions in MVC controllers.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684955" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the “event” pattern</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-event-pattern.aspx</link><pubDate>Sun, 05 Apr 2009 20:37:41 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684953</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684953</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/05/the-mvc-framework-the-event-pattern.aspx#comments</comments><description>&lt;p&gt;Today we’re going to keep looking at the MVC framework and we’ll see how we can use the “event” pattern for invoking an action asynchronously. This pattern is really simple. You start by adding two methods: the first is responsible for kicking off the asynchronous processing and the second will be called when all the asynchronous actions complete (as you’ll see, with this approach you can start several asynchronous actions). The ending method’s name should follow a specific convention: it must end with the word Completed (where XXX is the name of the action method that is exposed by your controller) and it should return the ActionResult object responsible for kicking off the view rendering phase. &lt;/p&gt;  &lt;p&gt;When you opt for this approach, you’ll have to notify the framework whenever you start an asynchronous task (and you also need to signal its end). In practice, this means that you’ll have to increment the AsyncManager’s OutsandingOperations property whenever you start an asynchronous action. And then you’ll also have to decrement this property when that async operation is completes. Internally, this property delegates to a counter and when that counter reaches 0, the platform will automatically invoke your XXXCompleted method.&lt;/p&gt;  &lt;p&gt;Your XXXCompleted method might also receive parameters that are automatically fed with the values that might have been added to the AsyncManager’s Parameters property. In practice, when the platform invokes the XXXCompleted method, it will try to match the expected XXXCompleted parameters with values that might exists on the AsyncManager’s Parameters property. Those parameters which don’t exist in that Parameters property get their default value (default(T)).&lt;/p&gt;  &lt;p&gt;The restrictions presented for the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/04/02/the-mvc-framework-the-iasyncresult-pattern.aspx"&gt;IAsyncResult pattern&lt;/a&gt; applies to this option too. That means that filters must be applied to the begin method (and not to the end method) and that the end method must match the real method name (instead of an eventual alias name that might have been applied to that method). Enough theory…it’s time to show some code. We’re going to reuse the previous example and change it so that we apply this pattern:&lt;/p&gt;  &lt;p&gt;public void GetHtml(String url) {   &lt;br /&gt;&amp;#160; var req = WebRequest.Create(url);    &lt;br /&gt;&amp;#160; req.Method = &amp;quot;GET&amp;quot;;    &lt;br /&gt;&amp;#160; AsyncManager.OutstandingOperations.Increment();&amp;#160; &lt;br /&gt;&amp;#160; req.BeginGetResponse(    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (ar) =&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var response = req.EndGetResponse(ar);    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; using (var reader = new StreamReader(response.GetResponseStream())) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AsyncManager.Parameters[&amp;quot;contents&amp;quot;] = reader.ReadToEnd();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AsyncManager.OutstandingOperations.Decrement();    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; },    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; null);    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;public ActionResult GetHtmlCompleted(String contents) {   &lt;br /&gt;&amp;#160; ViewData[&amp;quot;contents&amp;quot;] = contents;    &lt;br /&gt;&amp;#160; return View(&amp;quot;Index&amp;quot;);    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;There are a couple of interesting observations that you can take from this example:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;we increment the OustandingOperations before kicking the asynchronous operation;&lt;/li&gt;    &lt;li&gt;at the end of the callback method, we decrement the OutstandingOperations. When it reaches 0, the GetHtmlCompleted method is called;&lt;/li&gt;    &lt;li&gt;notice that we use the Parameters property to set the contents parameter value. btw, if we wanted we could also add an entry to the ViewData dictionary instead of doing that in the XXXCompleted method.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Even though the sample has only kicked off one asynchronous operation, nothing prevents you from starting several asynchronous operations. The only thing you can’t forget to do is to keep track of the current running asynchronous operations by incrementing and decrementing the OutstandingOperations property.&lt;/p&gt;  &lt;p&gt;And that’s it for today. More about asynchronous &lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684953" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the IAsyncResult pattern</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/the-mvc-framework-the-iasyncresult-pattern.aspx</link><pubDate>Thu, 02 Apr 2009 21:19:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684362</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684362</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/the-mvc-framework-the-iasyncresult-pattern.aspx#comments</comments><description>&lt;p&gt;In this post we’re going to keep looking at the asynchronous features introduced by the MVC futures and we’ll see an example of how to use the IAsyncResult pattern for adding an asynchronous action to a controller. The idea is simple: you need to add two methods with the following signatures:&lt;/p&gt;  &lt;p&gt;public IAsyncResult BeginActionMethod(Int32 id, AsyncCallback callback, Object state);   &lt;br /&gt;public ActionResult EndActionMethod(IAsyncResult asyncResult);&lt;/p&gt;  &lt;p&gt;This is the expected asynchronous version of the traditional synchronous methods you generally end up using on the ASP.NET MVC applications. To illustrate its use, we’re going to change the default HomeController you get when you create a new ASP.NET MVC project and add a new method which gets the HTML returned from an existing url. Lets start with the changes we’ll be doing to the Index view:&lt;/p&gt;  &lt;p&gt;% using( var frm = Html.BeginForm( &amp;quot;GetHtml&amp;quot;, &amp;quot;Home&amp;quot;) )   &lt;br /&gt; {%&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;label for=&amp;quot;url&amp;quot;&amp;gt;Url: &amp;lt;/label&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;%=Html.TextBox(&amp;quot;url&amp;quot;)%&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Get html&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;lt;%    &lt;br /&gt; }%&amp;gt;&lt;/p&gt;  &lt;p&gt;Nothing new here: just a simple form which submits itself to a GetHtml action method on the Home controller. Now, the interesting part. Here’s the code we’ve added to the controller:&lt;/p&gt;  &lt;p&gt;public IAsyncResult BeginGetHtml(String url, AsyncCallback cb, Object state)&amp;#160; {   &lt;br /&gt;&amp;#160; var req = WebRequest.Create(url&lt;a href="http://www.microsoft.com&amp;quot;);"&gt;);&lt;/a&gt;    &lt;br /&gt;&amp;#160; req.Method = &amp;quot;GET&amp;quot;;&amp;#160; &lt;br /&gt;&amp;#160; return req.BeginGetResponse(cb, req);    &lt;br /&gt;} &lt;/p&gt;  &lt;p&gt;public ActionResult EndGetHtml(IAsyncResult ar) {   &lt;br /&gt;&amp;#160; var req = ar.AsyncState as WebRequest;    &lt;br /&gt;&amp;#160; var response = req.EndGetResponse(ar);    &lt;br /&gt;&amp;#160; using (var reader = new StreamReader( response.GetResponseStream() ) ) {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ViewData[&amp;quot;contents&amp;quot;] = reader.ReadToEnd();    &lt;br /&gt;&amp;#160; }    &lt;br /&gt;&amp;#160; return View( &amp;quot;Index&amp;quot; );    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;The most interesting thing is that the method signature can be slightly changed so that you get all the goodies you’re used to in the synchronous model (ie, you can still have automatic binding between the method’s parameters and the form’s submitted values).&lt;/p&gt;  &lt;p&gt;As you can see, we’re returning the IAsyncResult object that resulted from the call to the BeginGetResponse method (notice also the chain of callbacks in the BeginGetHtml method: we don’t pass our callback method to it; instead, we’re passing the callback method that we’ve received in our method – parameter cb). The EndGetHtml method is also pretty simple: we’re just getting the response and translating it into a string. Finally, we return the ActionResult that will render our view. btw, you should also add something like this to the Index view:&lt;/p&gt;  &lt;p&gt;&amp;lt;%= Html.Encode(ViewData[&amp;quot;contents&amp;quot;]&amp;#160; ) %&amp;gt;&lt;/p&gt;  &lt;p&gt;This ensures that you’ll see the return HTML on your view.&lt;/p&gt;  &lt;p&gt;There are some things you should keep in mind when using this approach:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;filter attributes must be placed in the begin method;&lt;/li&gt;    &lt;li&gt;even though you can apply the ActionNameAttribute to the BeginXXX method, the End method must match the real method name (and not the alias you’ve created by using the ActionNameAttribute);&lt;/li&gt;    &lt;li&gt;You can’t call Begin methods directly when using asynchronous controllers. If you need that, you’ll have to use the previous attribute (ActionNameAttribute) to create that illusion.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;And that’s all for today…more on asynchronous calls in the next posts. Keep tuned.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684362" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: invoking actions asynchronously I</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/the-mvc-framework-invoking-actions-asynchronously-i.aspx</link><pubDate>Thu, 02 Apr 2009 18:34:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684291</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684291</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/the-mvc-framework-invoking-actions-asynchronously-i.aspx#comments</comments><description>&lt;p&gt;As we’ll see, the MVC futures assembly adds several classes which help you in executing asynchronous actions. In this post, we’ll take a quick look at the infrastructure code needed to start adding asynchronous actions to your controllers. As you’ll that it would be good to make a small incursion into the internals that support asynchronous requests in the MVC futures assembly. If ysee (when we’re done with this series), the futures assembly gives you several options to perform an action asynchronously. Before showing some code, I believe ou take a look at the MVC futures source code, you’ll see that it has several classes that start with or contain the &lt;strong&gt;Async&lt;/strong&gt; word. Those classes are responsible for supporting the features we’ll be seeing in this and in the next posts.&lt;/p&gt;  &lt;p&gt;Ok, nothing better than go back to the basics…as you &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/07/04/the-routing-series.aspx"&gt;recall&lt;/a&gt;, the routing module is really important and introduces the hooking point from where the MVC framework does its work. By default, when you register a route you end up using a helper method that associates a route with an instance of type MvcRouteHandler. As we’ve seen in the past, the only role of this route handler is to return the IHttpHandler that will process the request. Whenever you want to use asynchronous actions in your MVC app, you’ll need to associate a route with a different route handler: in this case, you’re interesting in using the MvcAsyncRouteHandler.&lt;/p&gt;  &lt;p&gt;If you want, you can pass a reference to this route handler when you create a new Route object (I call this the manual – but more explicit! - approach). However, you’ll probably prefer the quickest option which consists in using the RouteCollection extension methods defined in the static AsyncRouteCollectionExtensions class. As you might expect, you’ll find several overloads which let you set several of the properties of the Route you’re creating. &lt;/p&gt;  &lt;p&gt;The MvcAsyncRouteHandler is really similar to the existing MvcRouteHandler. However, in this case, it will return an instance of type MvcAsyncHandler that is responsible for handling the current request. This is an asynchronous handler (ie, it implements the IHttpAsyncHandler) and it also extends the MvcHandler class by adding a new property (SynchronizationContext) used internally for context propagation/synchronization (by default, it ends up using the default ASP.NET synchronization context).&lt;/p&gt;  &lt;p&gt;The implementation of the BeginProcessRequest and EndProcessRequest methods are fairly simple. The BeginProcessRequest method will instantiate the correct controller (it uses our old friend ControllerBuilder to instantiate the requested controller) and see if it implements the IAsyncController interface. If it does, then it will handle the current request asynchronously. If it doesn’t then it will simply execute that request synchronously (by invoking the SynchronizationContext.Sync method). The IAsyncController is a simple interface which adds two new methods to the ones defined on the IController method:&lt;/p&gt;  &lt;p&gt;public interface IAsyncController : IController {&amp;#160; &lt;br /&gt;IAsyncResult BeginExecute(RequestContext requestContext, AsyncCallback callback, object state);     &lt;br /&gt;void EndExecute(IAsyncResult asyncResult);&amp;#160; &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;There’s really not much to say about these methods…as you might expect, the BeginExecute method will kick off the asynchronous processing and the EndExecute will be called for cleaning everything up. The easiest way to implement this interface is to inherit from the AsyncController class. Again, this class builds on the existing Controller class and adds a very interesting property (which we’ll talk more about in the future): the AsyncManager property. This property (of type AsyncManager) can be used to register asynchronous tasks and we’ll return to this topic in future posts.&lt;/p&gt;  &lt;p&gt;Internally, the class will end up using several other auxiliary classes that are responsible for setting up the scene and trying to understand which asynchronous pattern should be used for handling the current request. I’m tempted to say that the one of the most important pieces of work is performed by the AsyncActionMethodSelector. This class is responsible for deciphering the current pattern and creating the correct action descriptor that will be responsible for invoking the current method (it ends up delegating to the traditional ReflectedActionDescriptor when none of the current available patters are found).&lt;/p&gt;  &lt;p&gt;In the previous paragraphs, I’ve mention several times asynchronous patterns. You’ll be happy to know that the current version of the future bits (at least the one I’ve downloaded to my machine) supports three different asynchronous patterns:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;you can define two methods (BeginXXX and EndXXX) on your controller; &lt;/li&gt;    &lt;li&gt;you can also expose two methods (XXX and XXXCompleted) that handle the beginning and ending of the processing (a similar approach to the async event pattern, but without events :) ); &lt;/li&gt;    &lt;li&gt;finally, there’s a continuation pattern where you can return a delegate from the action method that gets things started. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Before ending, there’s still time for an additional note: if you’re using IIS 6, then don’t forget to add the following to your web.config file:&lt;/p&gt;  &lt;p&gt;&amp;lt;add verb=&amp;quot;*&amp;quot; path=&amp;quot;*.mvc&amp;quot; validate=&amp;quot;false&amp;quot; type=&amp;quot;Microsoft.Web.Mvc.MvcHttpAsyncHandler, Microsoft.Web.Mvc&amp;quot;/&amp;gt;&lt;/p&gt;  &lt;p&gt;I think this is getting rather big and I’ll postpone some samples for the next posts :) Stay tuned for more on the ASP.NET MVC.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684291" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>Asynchronous controllers in MVC</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/asynchronous-controllers-in-mvc.aspx</link><pubDate>Thu, 02 Apr 2009 07:40:36 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1684050</guid><dc:creator>luisabreu</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1684050</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/04/02/asynchronous-controllers-in-mvc.aspx#comments</comments><description>&lt;p&gt;In these last days I’ve been busy with the source code of the futures assembly. I’ve already written one post (not published yet) and was planning on writing a couple more of them that showed how to use the asynchronous features introduced by the future bits. Well, it seems like my posts have just become redundant (though I’ll probably publish them anyway) because there’s a new document on &lt;a href="http://www.codeplex.com/"&gt;codeplex&lt;/a&gt; which explains how to &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471"&gt;use it&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1684050" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the ContentTypeAttribute attribute</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/27/the-mvc-framework-the-contenttypeattribute-attribute.aspx</link><pubDate>Thu, 26 Mar 2009 23:07:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1681787</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1681787</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/27/the-mvc-framework-the-contenttypeattribute-attribute.aspx#comments</comments><description>&lt;p&gt;Another short post on the features available on the MVC futures assembly…today, we’re going to take a look at the ContentTypeAttribute. This attribute is an action filter attribute and you can use it to setup the content type of the current response (ie, you can apply this attribute to an action method to influence the value of the ContentType property of the HttpResponseBase object). The constructor of the this type receives a string which identifies the content type of the response.&lt;/p&gt;  &lt;p&gt;Internally, this action filter ends up overriding the OnResultExecuting and OnResultExecuted methods in order to set the HttpResponseBase’s ContentType property to the value you’ve passed to its constructor. &lt;/p&gt;  &lt;p&gt;And that’s it. Keep tuned for more on the MVC…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1681787" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: pay one take several</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/25/the-mvc-framework-pay-one-take-several.aspx</link><pubDate>Wed, 25 Mar 2009 22:12:38 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1681435</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1681435</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/25/the-mvc-framework-pay-one-take-several.aspx#comments</comments><description>&lt;p&gt;Ok, not really! It’s still free…today I didn’t really had the time to dig into the stuff I wanted to investigate. So, I’ll just present several interesting topics about the MVC futures assembly:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Binders: the MVC future has some offers in this area. Let’s start with the ByteArrayModelBinder. This binder will recover a byte array from an existing field (as you might expect, it will try to interpret the value as a base 64 string). Before using this binder, don’t forget to register it with the ModelBinders.Binders collection. There’s also a SkipBindingAttribute which you can use to skip binding on an action controller’s parameter;&lt;/li&gt;    &lt;li&gt;Extensions: besides the extensions we’ve seen in the previous posts, there are a couple of other extensions which might be useful to you. Let’s start with the MailToExtensions. As you might expect from the name, these HtmlHelper method extensions let you add mailto links to your web views. There are several overloads which let you specify mail, subject, cc, bcc and body for the message. The ExpressionInputExtensions class adds several helpers which let you add textboxes, hidden fields, dropdowns, textareas and validation messages to a view. The main difference when compared to the ones present on the main MVC assembly is that these methods use Expressions for specifying the association between the generated field and a model property. If you’re looking for radio button helpers, then you’ll be pleased to know that the futures assembly has several helpers that might help you with that. RadioListExtensions is the class you want for this job. Items that are to be rendered should be passed through a collection of SelectListItem;&lt;/li&gt;    &lt;li&gt;Caching: the futures assembly introduces a class (CacheExtensions) which lets you specify a method that should be called for rendering portions of the page that shouldn’t be cached. Internally, you’ll end up using the WriteSubstitution method of the HttpResponseBase class (which, in practice, means that you’ll end up calling that &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.writesubstitution.aspx"&gt;method&lt;/a&gt; over the HttpResponse class).&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Since we still haven’t talked about using caching on MVC apps, I though this would be a good time for presenting a small example which illustrates the use to the CacheExtensions’ static methods. Lets suppose we’ve got a view which is cached. However, there’s a section which shouldn’t be cached, ie, there’s a specific portion of the view that should be regenerated for all the requests. Here’s how you might do that with this extension method (I’m just putting the view):&lt;/p&gt;  &lt;p&gt;&amp;lt;%@ Page Language=&amp;quot;C#&amp;quot; MasterPageFile=&amp;quot;~/Views/Shared/Site.Master&amp;quot; Inherits=&amp;quot;System.Web.Mvc.ViewPage&amp;quot; %&amp;gt;   &lt;br /&gt;&lt;strong&gt;&amp;lt;%@ OutputCache VaryByParam=&amp;quot;None&amp;quot; Duration=&amp;quot;60&amp;quot; %&amp;gt;&lt;/strong&gt;    &lt;br /&gt;&amp;lt;%@ Import Namespace=&amp;quot;MVCRTM.Controllers&amp;quot;%&amp;gt;    &lt;br /&gt;&amp;lt;%@ Import Namespace=&amp;quot;Microsoft.Web.Mvc&amp;quot;%&amp;gt; &lt;/p&gt;  &lt;p&gt;&amp;lt;asp:Content ID=&amp;quot;indexTitle&amp;quot; ContentPlaceHolderID=&amp;quot;TitleContent&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; Home Page    &lt;br /&gt;&amp;lt;/asp:Content&amp;gt; &lt;/p&gt;  &lt;p&gt;&amp;lt;asp:Content ID=&amp;quot;indexContent&amp;quot; ContentPlaceHolderID=&amp;quot;MainContent&amp;quot; runat=&amp;quot;server&amp;quot;&amp;gt;&amp;#160;&amp;#160; &lt;br /&gt;&amp;lt;form&amp;gt;    &lt;br /&gt;&amp;#160; &amp;lt;%= Html.SubmitButton( &amp;quot;Message&amp;quot;, &amp;quot;hi&amp;quot; ) %&amp;gt;    &lt;br /&gt;&amp;lt;/form&amp;gt;    &lt;br /&gt;&amp;lt;%= DateTime.Now %&amp;gt; &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&amp;lt;% Html.Substitute(&amp;#160; ctx =&amp;gt; DateTime.Now.ToString() );%&amp;gt;     &lt;br /&gt;&lt;/strong&gt;&amp;lt;/asp:Content&amp;gt;&lt;/p&gt;  &lt;p&gt;Ok, this is just a really simple (and dumb!) view with a form whose sole objective is to force a refresh of the view. Without caching, you should see both times being refreshed. However, after adding the cache directive, you should notice that only the second date will be updated. Notice that the Substitute method expects a MvcSubsitutionCallback delegate, which looks like this:&lt;/p&gt;  &lt;p&gt;public delegate string MvcSubstitutionCallback(HttpContextBase httpContext);&lt;/p&gt;  &lt;p&gt;As you can see, you cam access the current context from within the method you’re passing to handle the substitution.&lt;/p&gt;  &lt;p&gt;And that’s all for today. Keep tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1681435" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MCV framework: view extensions</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/24/the-mcv-framework-view-extensions.aspx</link><pubDate>Tue, 24 Mar 2009 22:17:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1681123</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1681123</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/24/the-mcv-framework-view-extensions.aspx#comments</comments><description>&lt;p&gt;Ok, here we are again for another episode on the inner workings of the MVC futures assembly. Today, we’re going to take a look at the HtmlHelper extension methods defined on the ViewExtension class. The current release of this class introduces two (static extension) methods which you can use to execute an action “in-place” during the current request:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;RenderRoute: this method lets you pass a RouteValueDictionary which is added to a RouteData object. Then, the method uses an auxiliary handler to execute the new request “in-line”;&lt;/li&gt;    &lt;li&gt;RenderAction/RenderAction&amp;lt;T&amp;gt;: there are several overloads of these methods. As you might expect, you’ll use them whenever you need to render a specific action of a controller. The main difference (when compared with the previous method) is that in this case you’ll end up getting two more entries on the RouteData’s Value dictionary: action and controller.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These methods end up delegating the execution of the request to a private handler: RenderActionMvcHandler. This handler expands the MvcHandler and the only thing it does is override AddVersionHeader so that it doesn’t set the custom HTTP version header. Ok, so it’s time to see this in action. Here’s a quick example that show how you can use it:&lt;/p&gt;  &lt;p&gt;&amp;lt;% Html.RenderAction&amp;lt;HomeController&amp;gt;(c =&amp;gt; c.Test()); %&amp;gt;&lt;/p&gt;  &lt;p&gt;In this case, we’re rendering the Test action method of the HomeController “in-place” from within the view (yes, that call is made from within a view – not really good, right? more about it below). Do notice that the method tries to “emulate” a new request and things might not work for anything that falls out of the “simple staff” scenario. For instance, here’s a couple of things you should keep in mind if you intend to use this method:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;it goes against the “spirit” of the pattern. Using this method means that you’re calling an action from a view (and in my opinion, that is not really a good thing);&lt;/li&gt;    &lt;li&gt;the faked request might not have everything you might need. Since it doesn’t know (beforehand) what you need and what you want to replace you’ll get a predefined default behavior.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Ok, so now that you know that it has some short comes, you’re free to use it at your own risk :)&lt;/p&gt;  &lt;p&gt;And that’s all for today. Keep tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1681123" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: maintaining TempData on cookies</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/23/the-mvc-framework-maintaining-tempdata-on-cookies.aspx</link><pubDate>Mon, 23 Mar 2009 22:14:33 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1680714</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1680714</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/23/the-mvc-framework-maintaining-tempdata-on-cookies.aspx#comments</comments><description>&lt;p&gt;A few months back, we’ve &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/07/31/the-mvc-framework-the-tempdata-property.aspx"&gt;seen how TempData&lt;/a&gt; is implemented and how it should be used. Today we’re going to keep looking at the future assembly and we’ll take a look at the CookieTempDataProvider class. The class implements the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/07/31/the-mvc-framework-the-tempdata-property.aspx"&gt;ITempDataProvider&lt;/a&gt; interface and you can use it it you intend to persist the temp data dictionary in cookies instead of relying in the traditional session temp data provider.&lt;/p&gt;  &lt;p&gt;Internally, the class will serialize the data using the BinaryFormatter and encode it into a base 64 string which is then set as the value of a cookie which is added to the current response. Then, when the browser asks for a specific controller action and starts a subsequent request to the server, this temp data provider will search the request and see if there’s cookie with the previous persisted temp data dictionary (btw, it will search for a cookie named __ControllerTempData). If it finds one, it will rehydrate the temp data dictionary from that cookie’s value and then it will expire the cookie immediately. It will also search for an existing cookie on the response and will also invalidate it by clearing its content and expiring it (like it did to the one present on the request’s cookie collection).&lt;/p&gt;  &lt;p&gt;The only thing you need to do is set the TempDataProvider that your controller class inherits from the Controller class. You have several options here. The important thing is that you shouldn’t set that value from the constructor of your controller because at that time your controller context is still null. A valid option could be overriding the Initialize method and set the TempDataProvider property:&lt;/p&gt;  &lt;p&gt;protected override void Initialize(RequestContext requestContext){   &lt;br /&gt;&amp;#160; base.Initialize(requestContext);    &lt;br /&gt;&amp;#160; TempDataProvider = new CookieTempDataProvider( requestContext.HttpContext );    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;And that’s it for today! Stay tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1680714" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: more on the future’s helpers</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-more-on-the-future-s-helpers.aspx</link><pubDate>Sun, 22 Mar 2009 21:54:27 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1680484</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1680484</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-more-on-the-future-s-helpers.aspx#comments</comments><description>&lt;p&gt;Ok, so in the previous posts I kept talking about some of the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-the-button-helper.aspx"&gt;helpers available&lt;/a&gt; on the future assembly. In this short post we’re going to complement that information with some more methods that let you expressions for specifying form actions (yes, this can be seen as &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/12/26/the-mvc-platform-forms-extensions.aspx"&gt;form&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/01/31/the-mvc-platform-more-form-extension-methods.aspx"&gt;extensions&lt;/a&gt;, part III).&lt;/p&gt;  &lt;p&gt;The future assembly introduces 2 new extension methods with these signatures:&lt;/p&gt;  &lt;p&gt;public static MvcForm BeginForm&amp;lt;TController&amp;gt;(this HtmlHelper helper, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action) where TController : Controller&lt;/p&gt;  &lt;p&gt;public static MvcForm BeginForm&amp;lt;TController&amp;gt;(this HtmlHelper helper, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, FormMethod method) where TController : Controller&lt;/p&gt;  &lt;p&gt;As you can see, they’re really similar to the ones &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2008/12/26/the-mvc-platform-forms-extensions.aspx"&gt;we have&lt;/a&gt; on the MVC assembly; the main difference, is that we’re using Lambdas for setting up the action that ends up deciding the url for the form’s submission. The use of this method follows the same approach we’ve &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-the-button-helper.aspx"&gt;seen&lt;/a&gt; for the button helper methods. And that’s it. There’s still more to come. Stay tuned!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1680484" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: the Button helper</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-the-button-helper.aspx</link><pubDate>Sun, 22 Mar 2009 14:05:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1680440</guid><dc:creator>luisabreu</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1680440</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/22/the-mvc-framework-the-button-helper.aspx#comments</comments><description>&lt;p&gt;Today we’re going to keep looking at other helps available on the future assembly. The topic for today is: ButtonBuilder and using the available extension methods for adding HTML button type elements to the page. We’re going to start looking at the functionalities available on the ButtonBuilder class (keep in mind that you won’t probably be calling these methods from the pages; generally, you’ll end up using the extension methods we’ll be presenting at the end of the post).&lt;/p&gt;  &lt;p&gt;Currently, the ButtonBuilder (static) class exposes 3 (static) methods:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;SubmitButton: used for inserting a &amp;lt;input type=”submit”&amp;gt; element to a form;&lt;/li&gt;    &lt;li&gt;SubmitImage: used for generating a &amp;lt;input type=”image”&amp;gt; element on a page;&lt;/li&gt;    &lt;li&gt;Button: used for adding &amp;lt;button&amp;gt; elements to a page.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here’s a quick example of how you might use the Button method to add a &amp;lt;button&amp;gt; element and to configure it so that it shows an alert message when someone clicks on that button:&lt;/p&gt;  &lt;p&gt;&amp;lt;%= Html.Button( &amp;quot;message&amp;quot;,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;H&amp;quot;,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; HtmlButtonType.Button,&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;printHello()&amp;quot;) %&amp;gt;&lt;/p&gt;  &lt;p&gt;As you can see, we’re using one of the HtmlHelper extension methods defined on the ButtonsAndLinkExtensions static class. Besides this method (and its overloads), the class exposes also several other methods that redirect to the methods presented on the previous list.&lt;/p&gt;  &lt;p&gt;One of the things you should keep in mind is that, in this case, the name parameter is used only to set up the HTML name (or id) attributes on the generated HTML control. So, if you need to pass a value from the view data dictionary, you’ll have to do it explicitly.&lt;/p&gt;  &lt;p&gt;And that’s it. There’s still more to say about the MVC framework, but I’ll leave it to another day…&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1680440" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: more on links</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/21/the-mvc-framework-more-on-links.aspx</link><pubDate>Sat, 21 Mar 2009 22:55:03 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1680344</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1680344</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/21/the-mvc-framework-more-on-links.aspx#comments</comments><description>&lt;p&gt;Ok, now that &lt;a href="http://www.codeplex.com/"&gt;codeplex&lt;/a&gt; is up again, I’ve already downloaded the &lt;a href="http://www.codeplex.com/aspnet"&gt;latest bits&lt;/a&gt; for the ASP.NET MVC framework. In the past, we’ve already seen that there are several helpers for generating links for specific actions. As we’re going to see today, the future assemblies adds more ActionLink extension method helpers that let you specify the url of an anchor in a strongly typed manner (by using Expression&amp;lt;T&amp;gt; to define the associated action instead of relying on strings). &lt;/p&gt;  &lt;p&gt;As you might expect, the first thing we need to do is add a reference to the future assembly in our MVC project. Then, we’ll start by adding a reference to the Microsoft.Web.Mvc namespace on the view by using the &lt;a href="http://msdn.microsoft.com/en-us/library/eb44kack.aspx"&gt;Import&lt;/a&gt; directive:&lt;/p&gt;  &lt;p&gt;&amp;lt;%@ Import Namespace=&amp;quot;Microsoft.Web.Mvc&amp;quot; %&amp;gt;&lt;/p&gt;  &lt;p&gt;Now, we can start using the new ActionLink extension in the page. Here’s a quick example (supposing we’ve got a ShowMessage action method on the HomeController class that receives a string parameter called msg):&lt;/p&gt;  &lt;p&gt;&amp;lt;%= Html.ActionLink&amp;lt;HomeController&amp;gt;(    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; c =&amp;gt;c.ShowMessage(“hi”), “Say hi!” );&lt;/p&gt;  &lt;p&gt;As you can see, we’re passing&amp;#160; an Action&amp;lt;T&amp;gt; expression (where T is a controller) which ends up being parsed and transformed into a RouteValueDictionary. In this case, we’re passing a constant value to the ShowMessage. When the expression isn’t a constant, it ends up being converted into a compiled Lambda Func&amp;lt;Object&amp;gt; which is invoked. btw, here’s the HTML generated by the previous snippet:&lt;/p&gt;  &lt;p&gt;&amp;lt;a href=”/Home/ShowMessage?msg=hi”&amp;gt;Say hi!&amp;lt;/a&amp;gt;&lt;/p&gt;  &lt;p&gt;And that’s it: by using this helper, you can build your anchors’ url in a strongly typed way. btw, and since we’re talking about additional link features introduced by the futures assembly, this post wouldn’t be complete without mentioning the BuildUrlFromExtension&amp;lt;T&amp;gt; method (an HtmlHelper extension method). The main difference (when compared with the previous method) is that this method will only return the url (instead of the complete HTML for the anchor).&lt;/p&gt;  &lt;p&gt;And that’s it for today. Keep tuned for more on the MVC framework.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1680344" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item><item><title>The MVC framework: working with uploaded files</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/03/17/the-mvc-framework-working-with-uploaded-files.aspx</link><pubDate>Tue, 17 Mar 2009 10:43:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1678617</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1678617</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/03/17/the-mvc-framework-working-with-uploaded-files.aspx#comments</comments><description>&lt;p&gt;Today we’re going to keep looking at the futures assembly. In a &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/02/12/the-mvc-platform-working-with-files.aspx"&gt;previous post&lt;/a&gt;, we’ve seen how we can easily return a file from an action method. Today, we’re going to take a look at how we can use the FileCollectionModelBinder to get access to a group of files that have been uploaded to the server. The first thing you need to do when you want to use this binder is to register it. The FileCollectionModelBinder introduces a static method that registers the binder for all types of collections off HttpPostFileBase items. We can do this from within application start event:&lt;/p&gt;  &lt;p&gt;protected void Application_Start() {   &lt;br /&gt;&amp;#160;&amp;#160; RegisterRoutes(RouteTable.Routes);    &lt;br /&gt;&amp;#160;&amp;#160; FileCollectionModelBinder.RegisterBinder( ModelBinders.Binders );    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;After doing this, all parameters of type HttpPostedFileBase[], IEnumerable&amp;lt;HttpPostedFileBase&amp;gt;, ICollection&amp;lt;HttpPostedFileBase&amp;gt;, IList&amp;lt;HttpPostedFileBase&amp;gt;, Collection&amp;lt;HttpPostedFileBase&amp;gt; and List&amp;lt;HttpPostedFileBase&amp;gt; will be automatically filled by this custom binder. In order to illustrate its use, we’ll start by adding a form to a view that lets you update files. Here’s the markup we’re using:&lt;/p&gt;  &lt;p&gt;&amp;lt;% using( var form = Html.BeginForm(   &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;ProcessFiles&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;Home&amp;quot;,    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FormMethod.Post,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; new { enctype = &amp;quot;multipart/form-data&amp;quot;}))     &lt;br /&gt;{%&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;files&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;files&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;files&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;files&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;input type=&amp;quot;submit&amp;quot; value=&amp;quot;Send files&amp;quot; /&amp;gt;    &lt;br /&gt;&amp;lt;% }%&amp;gt;&lt;/p&gt;  &lt;p&gt;As you can see, we need to add the enctype attribute “by hand” to the form tag. If you don’t do that, you’ll end up without no files on the server side. Another interesting thing is that we’re defining several &amp;lt;input type=”file”&amp;gt; elements and we’re giving them the same name. We’re doing this because we want to get them all on the server side. Here’s the code for the ProcessFiles method:&lt;/p&gt;  &lt;p&gt;public ActionResult ProcessFiles(IEnumerable&amp;lt;HttpPostedFileBase&amp;gt; files) {   &lt;br /&gt;&amp;#160; //do something with the files here    &lt;br /&gt;&amp;#160; return GetView();    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;If everything works out, files will give a collection with all the files submitted by the user on the browser. It’s important to keep in mind the rules associated with the binding associations: the parameter name must match the name of the files you want to retrieve form the form (in other words, make sure that the parameter name matches the name attribute you’ve passed to the input elements of type file you want to recover). I haven’t tested it, but this should also work with custom objects with properties that match one of the types handled by this binder.&lt;/p&gt;  &lt;p&gt;And that’s all for today. Keep tuned for more on MVC.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1678617" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/MVC/default.aspx">MVC</category></item></channel></rss>