Search code examples
.netfunctionodata

How to allow special characters (slashes and periods) in OData unbound function string parameter?


I am creating an unbound function into an OData v4 service in .net. The function is declared as follows:

WebApi Route Config:

    modelBuilder
       .Function("MyFunction1")
       .ReturnsCollection<string>()
       .Parameter<string>("parameter1");

Controller:

[HttpGet]
[ODataRoute("MyFunction1(parameter1={value1})")]
public IHttpActionResult MyFunction1([FromODataUri] string value1)
{
    return Ok(MyFunction2(value1));
}

It can be invoked like this:

/myservice/MyFunction1(parameter1='abcd')

This function works fine as long as parameter1 does not contain forward slashes ("/"). Because of the nature of the function, it is required to allow spaces, periods, and slashes. I was able to configure the service to allow spaces and periods with the following line in the web.config file (handlers section):

<add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

In the end, what I need is that the following calls do not throw error 500:

/myservice/MyFunction1(parameter1='ab/c d/efg')

Is there something that I'm missing in the web.config file configuration, or in the function declaration?

Thanks in advance!!


Solution

  • You may have trouble passing in parameters in the URL with forward slashes as the routing seems to read them literally even when they are encoded. To avoid this problem, you can pass all of the parameters in as an object in the request content. See this on the MS OData blog