2

I have an WebApi endpoint for a database table in which 3 columns are PKs. In some cases one of the keys is an empty string ('') in the database.

Is it possible to supply this empty string value in my request URL?

Example:

[RoutePrefix("api/Endpoint")]
public partial class APIUserCustomerController : BaseApiController<APIUserCustomer>
{
   [HttpDelete, Route("{UserName}/{CustomerNum}/{ShippingAddress}")]
   public HttpResponseMessage Delete(System.String UserName, System.String CustomerNum, System.String ShippingAddress) 
   {
       ...
   }
}

I've tried:

DELETE api/Endpoint/username/customerNum/, which gives me HTTP Error 404.0 - Not Found

as well as:

DELETE api/Endpoint/username/customerNum/%20 which gives me Server Error in '/' Application. The resource cannot be found.

Is there a solution that I'm missing, or am I just SOL in regards to empty strings in my db?

2
  • What does DELETE api/Endpoint/username/customerNum// do? or DELETE api/Endpoint/username/customerNum/%20/ Commented Aug 13, 2014 at 19:49
  • @Prescott, I don't think the code inside the method matters, the problem is that it's not finding the endpoint since the last piece of the URL is blank and therefore the api thinks it doesn't match any of the routes... but anyway, in this case it should delete the row in the database where User_Name = 'username' and Customer_Num = 'customerNum' and ShippingAddress = '' Commented Aug 13, 2014 at 21:05

1 Answer 1

2

Found a solution in optional parameters:

[RoutePrefix("api/Endpoint")]
public partial class APIUserCustomerController : BaseApiController<APIUserCustomer>
{
   [HttpDelete, Route("{UserName}/{CustomerNum}/{ShippingAddress?}")]
   public HttpResponseMessage Delete(System.String UserName, System.String CustomerNum, System.String ShippingAddress = "") 
   {
       ...
   }
}

Now I can call

DELETE api/Endpoint/username/customerNum/shippingAddress if I have the ship address

OR

DELETE api/Endpoint/username/customerNum if the ship address is blank.

Works great!!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.