<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Sayed Ibrahim Hashimi - MSBuild, Web Deploy (MSDeploy), ASP.NET - routing</title>
    <link>http://sedodream.com/</link>
    <description>MSBuild, C#, Visual Studio and more</description>
    <language>en-us</language>
    <copyright>Sayed Ibrahim Hashimi</copyright>
    <lastBuildDate>Fri, 02 Apr 2010 05:24:30 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>sayed.hashimi@gmail.com</managingEditor>
    <webMaster>sayed.hashimi@gmail.com</webMaster>
    <item>
      <trackback:ping>http://sedodream.com/Trackback.aspx?guid=beb40727-c878-440d-bb2d-80501bb77312</trackback:ping>
      <pingback:server>http://sedodream.com/pingback.aspx</pingback:server>
      <pingback:target>http://sedodream.com/PermaLink,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</pingback:target>
      <dc:creator>Ibrahim</dc:creator>
      <wfw:comment>http://sedodream.com/CommentView,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</wfw:comment>
      <wfw:commentRss>http://sedodream.com/SyndicationService.asmx/GetEntryCommentsRss?guid=beb40727-c878-440d-bb2d-80501bb77312</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
The story around <a href="http://msdn.microsoft.com/en-us/library/cc668201%28VS.100%29.aspx">routing</a> in
ASP.NET MVC is pretty good. When a resource is requested the action that it is routed
to is determined and the parameters to the action are initialized. Counter to that
when you build a link you specify the argument values and it creates the url for you.
So if your routes happen to change your application shouldn’t be bothered by that.
One area which can be problematic for this is if you are making any ajax requests.
What I’ve done to minimize the impact of route changes to Ajax requests is to have
the script make a call to determine the correct route. In the most trivial case this
is extremely easy. You call an action, which has a known route, passing into it the
name of the action and controller. It returns to you the url for the route. This works
really good, but the problem is when your routes have parameters, how do you handle
those?
</p>
        <p>
First let’s take a look at the Javascript and then the definition of the action method
which it calls.
</p>
        <pre class="brush: js;">// wire up the event for the enter button
$("#searchText").keypress(function (event) {
    if (event.keyCode == 13) {
        // grab the text that is inside the box
        var text = $("#searchText").val();

        var routData = {
            controllerName: 'Search',
            actionName: 'Search',
            paramValues: "{ criteria: '" + text + "' }"
        };

        $.ajax({
            url: "/Home/MapRoute",
            data: routData,
            success: function (data) {
                window.location.href = data;
            },
            error: function (data) {
                alert('there was a failure on the internets');
            }
        });

    }
});</pre>
        <p>
Here I am building a Javascript object named routeData. This is declared in <a href="http://json.org/">JSON</a>.
One thing to pay attention to is the fact that the value for <em><strong>paramValues</strong></em> is
a string containing JSON. This is passed to the controller action.
</p>
        <p>
I’m using <a href="http://jquery.com/">jQuery</a> to make an Ajax request to <em>/Home/MapRoute</em>.
This maps to the method shown below.
</p>
        <pre class="brush: csharp;">public JsonResult MapRoute(string actionName, string controllerName, string paramValues)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();

    Dictionary&lt;string, string&gt; parameters = jss.Deserialize&lt;Dictionary&lt;string, string&gt;&gt;(paramValues);

    RouteValueDictionary rd = new RouteValueDictionary();
    foreach (string key in parameters.Keys)
    {
        rd.Add(key, parameters[key]);
    }

    UrlHelper urlHelper = new UrlHelper(this.Request.RequestContext);
    string url = urlHelper.Action(actionName, controllerName, rd);

    return Json(url, JsonRequestBehavior.AllowGet);
}</pre>
        <p>
Here I’m using the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx">JavaScriptSerializer</a> to
convert the JSON string into a dictionary, with string as key and value. I use that
dictionary to create a <a href="http://msdn.microsoft.com/en-us/library/system.web.routing.routevaluedictionary.aspx">RouteValueDictionary</a> which
is passed, along with other parameters, into the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx">UrlHelper</a> to
generate the url. When you return the Json result you must specify <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28VS.100%29.aspx">JsonRequestBehavior</a>.AllowGet,
otherwise a <strong><em>500 internal service error</em></strong> will be returned.
I think this is new with ASP.NET 2.
</p>
        <p>
When the action method returns, you can use that url. The drawback to this approach
is that you will make an extra request to determine the url, but you will be sure
that those urls are correct. Also you could cache the results with <a href="http://www.asp.net/learn/mvc/tutorial-15-cs.aspx">Output
Caching</a> since the routes won’t change.
</p>
        <p>
Sayed Ibrahim Hashimi
</p>
      </body>
      <title>ASP.NET MVC Route + Ajax + jQuery</title>
      <guid isPermaLink="false">http://sedodream.com/PermaLink,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</guid>
      <link>http://sedodream.com/2010/04/02/ASPNETMVCRouteAjaxJQuery.aspx</link>
      <pubDate>Fri, 02 Apr 2010 05:24:30 GMT</pubDate>
      <description>&lt;p&gt;
The story around &lt;a href="http://msdn.microsoft.com/en-us/library/cc668201%28VS.100%29.aspx"&gt;routing&lt;/a&gt; in
ASP.NET MVC is pretty good. When a resource is requested the action that it is routed
to is determined and the parameters to the action are initialized. Counter to that
when you build a link you specify the argument values and it creates the url for you.
So if your routes happen to change your application shouldn’t be bothered by that.
One area which can be problematic for this is if you are making any ajax requests.
What I’ve done to minimize the impact of route changes to Ajax requests is to have
the script make a call to determine the correct route. In the most trivial case this
is extremely easy. You call an action, which has a known route, passing into it the
name of the action and controller. It returns to you the url for the route. This works
really good, but the problem is when your routes have parameters, how do you handle
those?
&lt;/p&gt;
&lt;p&gt;
First let’s take a look at the Javascript and then the definition of the action method
which it calls.
&lt;/p&gt;
&lt;pre class="brush: js;"&gt;// wire up the event for the enter button
$(&amp;quot;#searchText&amp;quot;).keypress(function (event) {
    if (event.keyCode == 13) {
        // grab the text that is inside the box
        var text = $(&amp;quot;#searchText&amp;quot;).val();

        var routData = {
            controllerName: 'Search',
            actionName: 'Search',
            paramValues: &amp;quot;{ criteria: '&amp;quot; + text + &amp;quot;' }&amp;quot;
        };

        $.ajax({
            url: &amp;quot;/Home/MapRoute&amp;quot;,
            data: routData,
            success: function (data) {
                window.location.href = data;
            },
            error: function (data) {
                alert('there was a failure on the internets');
            }
        });

    }
});&lt;/pre&gt;
&lt;p&gt;
Here I am building a Javascript object named routeData. This is declared in &lt;a href="http://json.org/"&gt;JSON&lt;/a&gt;.
One thing to pay attention to is the fact that the value for &lt;em&gt;&lt;strong&gt;paramValues&lt;/strong&gt;&lt;/em&gt; is
a string containing JSON. This is passed to the controller action.
&lt;/p&gt;
&lt;p&gt;
I’m using &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; to make an Ajax request to &lt;em&gt;/Home/MapRoute&lt;/em&gt;.
This maps to the method shown below.
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;public JsonResult MapRoute(string actionName, string controllerName, string paramValues)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();

    Dictionary&amp;lt;string, string&amp;gt; parameters = jss.Deserialize&amp;lt;Dictionary&amp;lt;string, string&amp;gt;&amp;gt;(paramValues);

    RouteValueDictionary rd = new RouteValueDictionary();
    foreach (string key in parameters.Keys)
    {
        rd.Add(key, parameters[key]);
    }

    UrlHelper urlHelper = new UrlHelper(this.Request.RequestContext);
    string url = urlHelper.Action(actionName, controllerName, rd);

    return Json(url, JsonRequestBehavior.AllowGet);
}&lt;/pre&gt;
&lt;p&gt;
Here I’m using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx"&gt;JavaScriptSerializer&lt;/a&gt; to
convert the JSON string into a dictionary, with string as key and value. I use that
dictionary to create a &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.routing.routevaluedictionary.aspx"&gt;RouteValueDictionary&lt;/a&gt; which
is passed, along with other parameters, into the &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.aspx"&gt;UrlHelper&lt;/a&gt; to
generate the url. When you return the Json result you must specify &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28VS.100%29.aspx"&gt;JsonRequestBehavior&lt;/a&gt;.AllowGet,
otherwise a &lt;strong&gt;&lt;em&gt;500 internal service error&lt;/em&gt;&lt;/strong&gt; will be returned.
I think this is new with ASP.NET 2.
&lt;/p&gt;
&lt;p&gt;
When the action method returns, you can use that url. The drawback to this approach
is that you will make an extra request to determine the url, but you will be sure
that those urls are correct. Also you could cache the results with &lt;a href="http://www.asp.net/learn/mvc/tutorial-15-cs.aspx"&gt;Output
Caching&lt;/a&gt; since the routes won’t change.
&lt;/p&gt;
&lt;p&gt;
Sayed Ibrahim Hashimi
&lt;/p&gt;</description>
      <comments>http://sedodream.com/CommentView,guid,beb40727-c878-440d-bb2d-80501bb77312.aspx</comments>
      <category>ASP.NET MVC</category>
      <category>Javascript</category>
      <category>jQuery</category>
      <category>routing</category>
    </item>
  </channel>
</rss>