1
votes

I can't get the replace function to work using odata, getting the following error: "Unknown function 'replace'"

So I pulled up the example url in their documentation, sure enough same error. http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=replace(CompanyName, ' ', '') eq 'AlfredsFutterkiste'

Is there an alternate way to do a replace?

2

2 Answers

1
votes

As per AllowedFunctions, Web API for V3 does not allow replace yet.

The sample service is using WCF Data Service, and it requires AcceptReplaceFunctionInQuery on DataServiceBehavior to be set true in order to support replace. The default value is false.

One approach is to use indexof in combine with substring to do replace, but this only works when the string to be replaced only appear once. See the following query:

http://services.odata.org/Northwind/Northwind.svc/Customers?$filter=indexof(CompanyName, ' ') ge 0 and concat(substring(CompanyName, 0, indexof(CompanyName, ' ')),substring(CompanyName, indexof(CompanyName, ' ') add 1, length(CompanyName))) eq 'AlfredsFutterkiste'
0
votes

I tried adding string.Replace to OData with https://github.com/OData/WebApi/issues/1937 but apparently according to Michael Pizzo:

We actually intentionally omit string.Replace because it was identified as a possible DOS attack surface. Using string.Replace, a bad actor could potentially create a request that would overflow the server.

Life was some much easier before we really had security...

The function is also no longer present in the specifications.