Permalink
Switch branches/tags
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
14 lines (11 sloc) 3.27 KB
---
layout: post
title: 'Using Rasta #2 - Rasta in it''s simplest form'
date: '2008-04-29T23:33:00.001+01:00'
tags: [openrasta, csharp]
modified_time: '2008-04-29T23:33:51.514+01:00'
blogger_id: tag:blogger.com,1999:blog-4015568221071268916.post-199026847547365533
comments: true
blogger_orig_url: http://serialseb.blogspot.com/2008/04/using-rasta-2-rasta-in-it-simplest-form.html
---
<p><em>Lack of time and sleep pushes me to be very lazy and present only the most trivial example of what Rasta was built for. Hopefully the future editions will become more interesting.</em></p> <p>Rasta does many things, but one of the things you can use it for is Url rewriting for webforms. Simply declare the following in your global.asax file:</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px"><span style="color: blue">using</span> (<span style="color: #2b91af">RastaConfiguration</span>.Manual)</p> <p style="margin: 0px">{</p> <p style="margin: 0px">&#160;&#160;&#160; <span style="color: #2b91af">UriSpace</span>.HasTheUri(<span style="color: #a31515">&quot;/home&quot;</span>).ForThePage(<span style="color: #a31515">&quot;~/pages/default.aspx&quot;</span>);</p> <p style="margin: 0px">}</p> </div> <p>And if you want to provide a url that matches a specific language, just expand this a bit:</p> <div style="font-size: 10pt; background: white; color: black; font-family: consolas, courier new"> <p style="margin: 0px"><span style="color: #2b91af">UriSpace</span>.HasTheUri(<span style="color: #a31515">&quot;/accueil&quot;</span>).InLanguage(<span style="color: #a31515">&quot;fr&quot;</span>).ForThePage(<span style="color: #a31515">&quot;~/pages/default.aspx&quot;</span>);</p> </div> <p>This will automatically set the language for the current request in the correct language, while still going to the same aspx.</p> <p>Another fancy thing in Rasta's manual declaration is the <em>using</em> block. All the configuration needs to happen within that block, and is only applied when you exit the block. The fluent api is built using a chain of language elements that form a sentence. Each element you add to a sentence either replace it's predecessor in the list of sentences, or initiate a new sentence, and each fully-formed sentence will get called whenever the Dispose method is called on the object returned by the call to <span style="color: #2b91af">RastaConfiguration</span>.Manual.</p> <p>How do sentences get added? When you enter the block, the new object returned by the Manual property sets a CallContext that stores that list. When it is disposed, it calls the chain of sentences to configure itself. And finally, it makes most of the routing structures read-only to optimise them (a typical example is building a read-only tree of the Urls you can use for a site in a structure that is more efficient than if it was write-only).</p> <p>That also means that trying to apply a configuration at runtime to replace the existing configuration will only succeed when the whole of the operation succeeds. This happens by only writing to a new copy of the configuration data, and committing the write if no exception happened. If you got the configuration wrong, the previous configuration will happily stay on to continue serving request.</p>