<?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</title>
	<atom:link href="http://www.arnesten.com/category/aspnet/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>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>
		<item>
		<title>Model View Controller from Microsoft</title>
		<link>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/</link>
		<comments>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 21:31:59 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/</guid>
		<description><![CDATA[Just read this. This is great news! I don&#8217;t like the way WebForms work today, I think its hard to get testable code close to the UI. I know there are alternatives like MonoRail, but I find the disadvantage of not having third party components like Telerik too big. But if a framework for Model [...]]]></description>
			<content:encoded><![CDATA[<p>Just read <a href="http://igloocoder.com/archive/2007/10/06/alt.net-day-2-session-3----ms-mvc-preview.aspx">this</a>. This is great news! I don&#8217;t like the way WebForms work today, I think its hard to get testable code close to the UI. I know there are alternatives like <a href="http://www.castleproject.org/monorail/">MonoRail</a>, but I find the disadvantage of not having third party components like <a href="http://www.telerik.com">Telerik</a> too big. But if a framework for Model View Controller comes from Microsoft, I&#8217;m sure that third party component companies will start develop components for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/10/06/model-view-controller-from-microsoft/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NHibernate 1.2 with Cuyahoga</title>
		<link>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/</link>
		<comments>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/#comments</comments>
		<pubDate>Fri, 25 May 2007 15:20:55 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Cuyahoga]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/</guid>
		<description><![CDATA[One of the things I missed when I started to look at Cuyahoga two weeks ago was that it didn&#8217;t use NHibernate 1.2. As I was used to NHibernate 1.2 from my work with its support for generics, I wanted to use that with Cuyahoga as well. So I decided to make a patch that [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I missed when I started to look at Cuyahoga two weeks ago was that it didn&#8217;t use NHibernate 1.2. As I was used to NHibernate 1.2 from my work with its support for generics, I wanted to use that with Cuyahoga as well. So I decided to make a patch that would enable me to use it.</p>
<p>Besides using the files from the NHibernate 1.2 release, I had to download the subversion source code for Castle.Facilities.NHibernateIntegration.dll. Then I had to make a few adjustments to the code for it to compile against NHibernate 1.2.</p>
<p>After I had upgraded the dll&#8217;s, the Cuyahoga compiled. But when I run it NHibernate complained on some things like missing virtuals and wrong mapping scheme (urn:nhibernate-mapping-2.0 instead of urn:nhibernate-mapping-2.2). After that it seems to be working nice.</p>
<p>You can download the patch for Cuyahoga 1.5 source code <a href='http://www.arnesten.com/wordpress/wp-content/patch-cuyahoga-150-nhibernate-12.zip' title='Patch-Cuyahoga-1.5.0-NHibernate 1.2.zip'>here.</a> And for Cuyahoga&#8217;s latest trunk <a href='http://www.arnesten.com/wordpress/wp-content/patch-cuyahoga-trunk-nhibernate-12.zip' title='Patch-Cuyahoga-trunk-NHibernate-1.2.zip'>here</a>. </p>
<p>Unfortunately, I&#8217;ve been having some problem applying the entire patches. It complains about some strange character in the beginning of some of the repository files, so you might have to manually edit some files. (The patch file is actually quite human-readable so it can be of help in the manual process). But for those that start from scratch I&#8217;ve also created a ready package of Cuyahoga 1.5 with patch applied. Download it <a href='http://www.arnesten.com/wordpress/wp-content/cuyahoga-150-with-nhibernate-12.zip' title='Cuyahoga-1.5.0-with-NHibernate-1.2.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/05/25/using-nhibernate-12-with-cuyahoga/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trouble when including JavaScript block with Telerik Ajax Panel</title>
		<link>http://www.arnesten.com/2007/05/14/trouble-when-including-javascript-block-with-telerik-ajax-panel/</link>
		<comments>http://www.arnesten.com/2007/05/14/trouble-when-including-javascript-block-with-telerik-ajax-panel/#comments</comments>
		<pubDate>Mon, 14 May 2007 16:28:52 +0000</pubDate>
		<dc:creator>Calle Arnesten</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.arnesten.com/?p=10</guid>
		<description><![CDATA[Today I was having quite a bit of problem when I wanted to include javascript from a control inside a Telerik Ajax Panel. The javascript in question was dynamically generated and should only be added sometimes depending on what action was made inside the ajax panel. The first thing I tried is the standard way [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was having quite a bit of problem when I wanted to include javascript from a control inside a <a href="http://www.telerik.com/demos/aspnet/Ajax/Examples/Panel/FirstLook/DefaultCS.aspx">Telerik Ajax Panel</a>. The javascript in question was dynamically generated and should only be added sometimes depending on what action was made inside the ajax panel.</p>
<p>The first thing I tried is the standard way of registering javascript blocks, namely using <a href="http://msdn2.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerclientscriptblock.aspx">Page.ClientScript.RegisterClientScriptBlock</a>. That didn&#8217;t work, which when I thought about it was logical as the script is added high up in the html and not inside the ajax panel. </p>
<p>Secondly, I read a bit on Telerik&#8217;s support pages. It suggested that I used RadAjaxPanel.EnableOutsideScripts and RadAjaxPanel.ResponseScripts. I didn&#8217;t get this to work either directly, and got outofmemory exception in javascript. Due to time constraints I didn&#8217;t investigate this properly. I will try and write and update soon, if I remember.</p>
<p>The last thing I tried was to just include the javascript inside the html of the ajax panel. Like the following code suggested I did this by using a LiteralControl, containing the script, that is inserted in the ajax panel&#8217;s control collection.</p>
<pre class="csharpcode">
RadAjaxPanel panel = <span class="kwrd">new</span> RadAjaxPanel();

<span class="kwrd">if</span> (someCondition)
{
  <span class="kwrd">string</span> myScript = <span class="str">"function MyAlert() { alert('Hello world'); }"</span>;
  LiteralControl scriptControl = <span class="kwrd">new</span> LiteralControl();
  script.Text = <span class="kwrd">string</span>.Format(
    <span class="str">@"&lt;script type="</span><span class="str">"text/javascript"</span><span class="str">"&gt;{0}&lt;/script&gt;"</span>,
    myFunction);
  panel.Controls.Add(scriptControl);

  Button alertButton = <span class="kwrd">new</span> Button();
  alertButton.OnClientClick(<span class="str">"MyAlert(); return false;"</span>);
  panel.Controls.Add(alertButton);
}</pre>
<p>For the sake of simplicity the javascript that is dynamically added in this example is rather static. This turned out to work. I should mention that this trick will work just as well with the standard Microsoft ASP.NET Ajax Update Panel. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.arnesten.com/2007/05/14/trouble-when-including-javascript-block-with-telerik-ajax-panel/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

