<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Calle Arnesten&#039;s Blog &#187; ASP.NET MVC</title>
	<atom:link href="http://www.arnesten.com/category/aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arnesten.com</link>
	<description>Web Development, Test Driven Development, NHibernate, ASP.NET MVC, jQuery and more..</description>
	<lastBuildDate>Sun, 10 Jan 2010 18:35:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.4</generator>
		<item>
		<title>Dumbbell.se är släppt</title>
		<link>http://www.arnesten.com/2008/10/12/dumbbellse-ar-slappt/</link>
		<comments>http://www.arnesten.com/2008/10/12/dumbbellse-ar-slappt/#comments</comments>
		<pubDate>Sun, 12 Oct 2008 16:00:50 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2008/10/12/dumbbellse-ar-slappt/</guid>
		<description><![CDATA[Igår la jag upp www.dumbbell.se för allmänheten. Dumbbell.se är en träningscommunity där man kan mata in sina träningspass, se statistik hur man utvecklas och diskutera med andra träningsintresserade människor. Siten kommer byggas ut gradvis närmsta tiden. Rent tekniskt så har jag använt mig av ASP.NET MVC, NHibernate och jQuery i en härlig symbios :) Jag [...]]]></description>
			<content:encoded><![CDATA[<p>Igår la jag upp <a href="http://www.dumbbell.se">www.dumbbell.se</a> för allmänheten. Dumbbell.se är en träningscommunity där man kan mata in sina träningspass, se statistik hur man utvecklas och diskutera med andra träningsintresserade människor. Siten kommer byggas ut gradvis närmsta tiden.</p>
<p>Rent tekniskt så har jag använt mig av ASP.NET MVC, NHibernate och jQuery i en härlig symbios :)</p>
<p>Jag kommer troligtvis skriva fler inlägg om dumbbell.se här i framtiden, men detta får vara tillräckligt för stunden.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/10/12/dumbbellse-ar-slappt/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Validating HTTP method in ASP.NET MVC</title>
		<link>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/</link>
		<comments>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 09:29:11 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/</guid>
		<description><![CDATA[On the web it&#8217;s considered a good practice that the GET method never changes the state on the server. When state should be changed the POST method should be used. It is good to follow this practice, otherwise there might be problems when search engines tries to visit links (which are GET by default). By [...]]]></description>
			<content:encoded><![CDATA[<p>On the web it&#8217;s considered a good practice that the GET method never changes the state on the server. When state should be changed the POST method should be used. It is good to follow this practice, otherwise there might be problems when search engines tries to visit links (which are GET by default).<br />
By default in ASP.NET MVC any public method on the controller can be accessed both by GET and POST. So I _could_ access my DeleteUser() method by an ordinary link (which is GET). </p>
<p>So, how do I restrict a method in ASP.NET MVC to be only GET or only POST? I could use ActionFilterAttribute and tag each method with it. But tagging every method is a bit noisy in my opinion. The thing I want is GET to be the default way to access methods, and then I want to mark POST methods only with attributes.</p>
<p>In my MVC projects I always have a BaseController, that all my controllers inherit from. With that in place it is actually quite easy to accomplish the above. I just need to override OnActionExecuting like this:</p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> BaseController : Controller
{
    <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnActionExecuting(ActionExecutingContext filterContext)
    {
        var attributes = filterContext.ActionMethod.GetCustomAttributes(
            <span class="kwrd">typeof</span>(POSTAttribute), <span class="kwrd">false</span>);
        var method = filterContext.HttpContext.Request.HttpMethod;
        <span class="kwrd">if</span> (attributes.Length &gt; 0 &amp;&amp; method != <span class="str">"POST"</span>)
        {
            ThrowIllegalMethodException(<span class="str">"POST"</span>, filterContext);
        }
        <span class="kwrd">else</span> <span class="kwrd">if</span> (attributes.Length == 0 &amp;&amp; method != <span class="str">"GET"</span>)
        {
            ThrowIllegalMethodException(<span class="str">"GET"</span>, filterContext);
        }
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ThrowIllegalMethodException(<span class="kwrd">string</span> expectedHttpMethod,
        ActionExecutingContext filterContext)
    {
        var controllerName = filterContext.Controller.GetType().Name;
        var controllerMethod = filterContext.ActionMethod.Name;
        var httpMethod = filterContext.HttpContext.Request.HttpMethod;

        var message = <span class="kwrd">string</span>.Format(<span class="str">"Only {0} method allowed for {1}.{2}, but {3} was used."</span>,
            expectedHttpMethod, controllerName, controllerMethod, httpMethod);
        <span class="kwrd">throw</span> <span class="kwrd">new</span> SecurityException(message);
    }
}</pre>
<p>And also I need a POSTAttribute which is simplest possible:</p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> POSTAttribute : Attribute
{
}</pre>
<p>So, now I can write [POST] above methods like this: </p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> MyController : BaseController
{
    [POST]
    <span class="kwrd">public</span> ActionResult SaveMyEntity()
    {
        <span class="rem">// Code..</span>
    }
}</pre>
<p>Pretty cool that you can change the behaviour of the MVC framework this easy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2008/07/10/validating-http-method-in-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

