Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constant Class for API Method Names

I am looking at a project that makes calls to an API (within C# code so this is taking place server-side). There are dozens of methods and so you might see something like /API/GetValue1 , /API/Search/ , /API/AnotherThing, or /API/Something/DoSomethingElse/1234411(some ID)

For all of this right now it's hard-coded into each of the calls, and likely won't change, but you never know.

Can all of this be moved into a constants class and so I could replace the occurence of /API/GetValue1 with something like MyConstantClass.Value1 and it would automatically put that as the URL path for the endpoint?

like image 817
user3520332 Avatar asked Dec 05 '25 14:12

user3520332


1 Answers

That's what you should do. Create a static class to contain all of your API endpoints. Never use magic strings directly in code, because they are error-prone.

like image 193
Toan Nguyen Avatar answered Dec 08 '25 08:12

Toan Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!