0

I have an issue with an api controller attribute routing. The API receives a get request containing a scanned barcode to the route: "api/[controller]/{userId}/{barcodeId}". Where the barcode is like: 0015A765248.

But we now have some new barcodes coming in this format: BBRT058/639.

I've found out through debugging that the extra / in the new barcode is causing the issue as the API controller is now not being accessed.

As I think it is trying to reach controller: "api/[controller]/{userId}/{barcodeId}/{barcodeID}"

Which doesn't exist. Is it possible to modify the API controller route attribute to accept the barcodeId containing the additional /?

Actually modifying the new barcode style in any way code wise is not an option I am told.

I have considered creating a new controller to accept the new barcode: "api/[controller]/{userId}/{barcodeIdpart1}/{barcodeIDpart2}" for example. But i'm not sure if creating a new controller that is a duplicate of the current one, with the only difference being the routing is a good idea. Also both barcode formats are likely still going to be used in the future.

  • 1
    Does this answer your question? [Route parameter with slash "/" in URL](https://stackoverflow.com/questions/30972578/route-parameter-with-slash-in-url) – Izzy May 24 '21 at 16:10
  • That's an answer for ASP.NET, not ASP.NET Core. – Andy May 24 '21 at 16:54
  • Keep in mind this is why query strings were invented. To pass data that can't be in the URL path. – Andy May 24 '21 at 16:56

2 Answers2

1

I don't believe you need to make any changes to your code/controller as such and that it seems to be correct. However, I would recommend you update the client to correctly URL encode the barcode before making the outbound call to your controller. This will ensure that the controller correctly parses the barcode and then processes it further. Additionally, this will also ensure that any other special non ASCII characters are correctly handled in the future.

This solution is not really changing the barcode format but is encoding it just before making the outbound HTTP call to correctly transmit it over the wire. Alternate solutions by hacking the controller will usually result in a non standard brittle solution and is not recommended.

Client calls URL

  • Current: api/controllername/userId/BBRT058/639
  • Proposed: api/controllername/userId/BBRT058%2F639

For further reading:

Kailash
  • 527
  • 4
  • 13
0

I would try to replace "/" with "!" or "!!" for example (you can select any sign or combination you like) - BBRT058!!639 before sending to the controller and replace back with "/" inside of the action.

barcodeId = barcodeId.Replace("!!", "/");

if you can't modify the barcode then try this

Route[("api/[controller]/{userId}/{barcodeId}/{barcodeIdpart2?})"]
public IActionResult GetBarcode(int userId, string barcodeId, string barcodeIdpart2 = null)
{

if  (!string.IsNullOrEmpty(barcodeIdpart2)) barcodeId=barcodeId + "/" + barcodeIdpart2;
....

}

It will work for both styles

Serge
  • 40,935
  • 4
  • 18
  • 45