Search code examples
asp.netajaxsitecorewebmethod

"Length cannot be less than zero. Parameter name: length" when calling ASPX WebMethod in site using Sitecore


I'm receiving "Length cannot be less than zero. Parameter name: length" errors when calling a WebMethod on a plain ASP.NET WebForms (ASPX) page. The error gives no clear idea (that I can see) as to what the problem is, and both GET and POST calls to other WebMethods with similar signatures on the page work.

I'm guess it's something in Sitecore causing the problem.

The full error in the response is:


[ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
   System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +10929307
   Sitecore.Web.RequestUrl.get_ItemPath() +138
   Sitecore.Pipelines.HttpRequest.SiteResolver.GetItemPath(HttpRequestArgs args, SiteContext context) +32
   Sitecore.Pipelines.HttpRequest.SiteResolver.UpdatePaths(HttpRequestArgs args, SiteContext site) +69
   Sitecore.Pipelines.HttpRequest.SiteResolver.Process(HttpRequestArgs args) +49
   Sitecore.Support.Pipelines.HttpRequest.SiteResolver.Process(HttpRequestArgs args) +261
   (Object , Object[] ) +59
   Sitecore.Pipelines.PipelineMethod.Invoke(Object[] parameters) +36
   Sitecore.Pipelines.CorePipeline.Run(PipelineArgs args) +237
   Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain, Boolean failIfNotExists) +158
   Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args, String pipelineDomain) +64
   Sitecore.Pipelines.CorePipeline.Run(String pipelineName, PipelineArgs args) +50
   Sitecore.Nexus.Web.HttpModule.(Object , EventArgs ) +450
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

My WebForms page is called "AddressLookupService.aspx:", located on the root of the site, and my WebMethod signature is (I also know it's nothing internal to the WebMethod causing the problem):


[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = false)]
public static string SearchAddressByFormattedAdd(string FormattedAddress, string SessionToken)
{
    ...
}

I'm calling the service from basic jQuery $.ajax() like this:


function CallAddressLookup() 
{
    var Result = null;

    var url = "/AddressLookupService.aspx/SearchAddressByFormattedAddress";
    var postData = { FormattedAddress: "123 Smith Street", SessionToken: "ABC" };

    $.ajax(
        {
            async: false,
            url: url,
            dataType: "json",
            data: JSON.stringify(postData),
            type: "POST",
            contentType: 'application/json; charset=utf-8',

            success: function (dataString, textStatus, jqXHR) 
            {
                Result = ...
            },

            error: function (jqXHR, textStatus, errorThrown) 
            {
                Result = ...
            }
        });

    return Result;
}

The site is one I've been brought in to integrate some new functionality into and it contains Sitecore code and configuration. I'm not a Sitecore developer and don't know the first thing about it, and this is definitely a "patch and pray" type project (I'm working mainly with heavy Javascript based UI and adding the WebMethods to keep new functionality separated).

What could be causing the error, and how can I fix it/work around it?

NOTE: See my first answer for a quick-fix I discovered.


Solution

  • Since you're working with Sitecore it will try to determine an item of its database matches with the requested URL before resolving to looking for it on disk. Itemnames in Sitecore have a maximal length (which is a setting you can find in the config files). Thus if your page name is longer then this maximum length you could run into this issue I believe.

    If you want to work outside of Sitecore using webmethods I'd advise you to setup a proper .svc or .asmx to host your JSON data instead of using .aspx files. This will cause Sitecore to leave the URLs alone. If you need access to sitecore data inside of these services leave a comment and I'll point you to the right posts here on stackoverflow.