{"id":4181,"date":"2016-07-30T08:19:55","date_gmt":"2016-07-30T08:19:55","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4181"},"modified":"2016-07-30T08:19:55","modified_gmt":"2016-07-30T08:19:55","slug":"passing-arguments-to-an-asp-net-mvc5-controller","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/passing-arguments-to-an-asp-net-mvc5-controller\/","title":{"rendered":"Passing arguments to an ASP.NET MVC5 controller"},"content":{"rendered":"<p>In our controller we might have a method along the lines<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic string Search(string criteria, bool ignoreCase = true)\r\n{\r\n   \/\/ do something useful\r\n   return $&quot;Criteria: {criteria}, Ignore Case: {ignoreCase}&quot;;\r\n}\r\n<\/pre>\n<p><em>Note: I&#8217;ve not bothered using HttpUtility.HtmlEncode on the return string as I want to minimize the code for these snippets.<\/em><\/p>\n<p>So we can simply create a query string as per<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:58277\/Music\/Search?criteria=something&ignoreCase=false\r\n<\/pre>\n<p>or we can add\/change the routing in RouteConfig, so for example in RouteConfig, RegisterRoutes we add<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nroutes.MapRoute(\r\n   name: &quot;Music&quot;,\r\n   url: &quot;{controller}\/{action}\/{criteria}\/{ignoreCase}&quot;\r\n);\r\n<\/pre>\n<p>now we can compose a URL thus<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:58277\/Music\/Search\/something\/false\r\n<\/pre>\n<p><em>Note: the routing names \/{criteria}\/{ignoreCase} <strong>must<\/strong> have the same names as the method parameters.<\/em> <\/p>\n<p>Obviously this example is a little contrived as we probably wouldn&#8217;t want to create a route for such a specific method signature.<\/p>\n<p>We might simply incorporate partial parameters into the routine, for example maybe all our MusicController methods took a citeria argument then we might use <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nroutes.MapRoute(\r\n   name: &quot;Music&quot;,\r\n   url: &quot;{controller}\/{action}\/{criteria}&quot;\r\n);\r\n<\/pre>\n<p><em>Note: there cannot be another route with the same number of parameters in the url preceding this or it will not be used.<\/em><\/p>\n<p>and hence our URL would like like<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nhttp:\/\/localhost:58277\/Music\/Search\/something?ignoreCase=false\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In our controller we might have a method along the lines public string Search(string criteria, bool ignoreCase = true) { \/\/ do something useful return $&quot;Criteria: {criteria}, Ignore Case: {ignoreCase}&quot;; } Note: I&#8217;ve not bothered using HttpUtility.HtmlEncode on the return string as I want to minimize the code for these snippets. So we can simply [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[128],"tags":[],"class_list":["post-4181","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4181"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4181\/revisions"}],"predecessor-version":[{"id":4187,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4181\/revisions\/4187"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}