1

Scenario

I have an ASP.NET Web API that returns a JSON object from my code-behind. The URL used to call this API supplies a delimited list of tag-ids (delimited by plus signs). The JSON object then returns posts that have this tag attached.

Everything was working great until I tried selecting all of the possible tags (sort of a stress-test on the API). Well turns out when I do this, I get a 400 (Bad Request) response. If remove a few parameters, there it goes working again. The URL length is around 270 characters before it breaks, but it seems to vary and I'm not sure if the error is caused by the length of the URL, or just too many parameters.

Question

When building an MVC 4 Web API, is there an XML tag in the web.config that I am missing that specifies the max URL length? Or perhaps a max number of parameters? Is there something in the Global.asax file I am missing?

Regarding the API, it is an MVC 4 Web API but is being used in a WebForms project in Visual Studio 2010. This means that there are some subtle differences, but even if someone can point me in the right direction, I'm sure I can solve this. At this point I just don't even know what to google :(

Code

Here is some code from my project in case it helps...

Global.asax (the "Posts" API is the one I am having trouble with. I renamed the others)

<%@ Application Language="VB" %>
<%@ Import Namespace="System.Web.Routing" %>
<%@ Import Namespace="System.Web.Http" %>

<script runat="server">


    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)



        RouteTable.Routes.MapHttpRoute(name:="OTHERAPI01", routeTemplate:="api/{controller}/{email}", defaults:=New With {.email = System.Web.Http.RouteParameter.Optional})
        RouteTable.Routes.MapHttpRoute(name:="OTHERAPI02", routeTemplate:="api/{controller}/{action}/{id}", defaults:=New With {.body = System.Web.Http.RouteParameter.Optional, .id = System.Web.Http.RouteParameter.Optional})
        RouteTable.Routes.MapHttpRoute(name:="OTHERAPI03", routeTemplate:="jjnotey/{controller}/{action}")
        RouteTable.Routes.MapHttpRoute(name:="Posts", routeTemplate:="postsApi/{controller}/{action}/{category}/{pageNumber}/{searchBy}/{searchValue}", _
                                       defaults:=New With {.category = "tb", .pageNumber = 1, .searchBy = System.Web.Http.RouteParameter.Optional, _
                                       .searchValue = System.Web.Http.RouteParameter.Optional})

    End Sub

</script>

The API function (Only the name and arguments. The content isn't relevant because the code execution doesn't even reach this function.)

Public Function GetPosts_NEW(ByVal category As String, ByVal pageNumber As Integer, _
                                     Optional ByVal searchBy As String = "", Optional ByVal searchValue As String = "") As String

The GET error (This is what the Google console is displaying for me)

GET http://localhost:49816/myDomainName/postsApi/Posts/GetPosts/tb/1/DepartmentId/1+2+3+4+5+6+7+8+9+10+11+12+13+14+15+16+17+18+19+20+21+22+23+24+25+26+27+28+29+30+31+32+33+34+35+36+37+38+39+40+41+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58+59+60+61+62+63+64+65+66+67+68+69+70+71+72+73+74+75+76+77+78+79+80+81+82+83+84+85+86+87+88+89+90+91+92+93+94+95+96+97+98+99+100+101+102+103+104 400 (Bad Request) 
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48
  • 1
    See if [this thread helps](http://stackoverflow.com/q/1185739/304683). – EdSF Dec 08 '13 at 06:48
  • @EdSF Yes it did thank you. I tried a couple of those answers and one of them got it to work. Read my answer to see the exact steps I took. – Ross Brasseaux Dec 08 '13 at 18:51

1 Answers1

0

Solved it

I solved this using Imingle's answer found here ASP.NET MVC, Url Routing: Maximum Path (URL) Length. It involved adding <httpRuntime maxUrlLength="1000" relaxedUrlToFileSystemMapping="true" /> to the web.config right below the <system.web> opening tag.


At least I believe this is what did the trick. My exact steps are as follows:

  1. Added a DWORD registry entry called UrlSegmentMaxLengh in the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\HTTP\Parameters. The value I set equal to 1000 (which equates to 4096 but I don't know much about that). -- TEST FAILED
  2. Restarted the computer. -- TEST FAILED
  3. Added <httpRuntime maxUrlLength="1000" relaxedUrlToFileSystemMapping="true" /> to the web.config right below the <system.web> opening tag. -- TEST PASSED

I hope it helps.

Community
  • 1
  • 1
Ross Brasseaux
  • 3,879
  • 1
  • 28
  • 48