4

I have this kind of sample code below that calls an API using get method:

CrudService.getAll("sampleurl/param1/param2/param3");

From that sample, param1 and param2 can be null/blank value. What happens is that the value of param3 goes to param1 when it comes to the API Controller.

How can I maintain the right values for the parameter even if there can be several null/blank values?

5
  • Are you using C# and ASP.NET Web API? If so, then please consider to add c# and asp.net-web-api tags to your question. There's a bigger chance to get answers if you use the correct / relevant tags. Commented Sep 11, 2018 at 11:24
  • If the input is a string wouldn't you just have to call "sampleurl///param3" this seems like bad design though, or i might be misunderstanding your question. Commented Sep 12, 2018 at 7:05
  • 1
    If you are in control of the web api, why not switch to adding parameters as query string and then you can extract via FromUri attrubute. See this: forums.asp.net/t/… Commented Sep 12, 2018 at 7:07
  • what is the web service that you use? Commented Sep 12, 2018 at 11:59
  • If you are in control of the service being called then you need to show the API controller action along with the routes in a minimal reproducible example. What you have shown so far is an XY problem and not enough for us to help you. Commented Sep 12, 2018 at 12:16

5 Answers 5

7
+25
CrudService.getAll("sampleurl/param1/param2/param3");

You have to pass a dummy value for param1 so if param1 is empty keep it "param1". various possible call based on empty params :

  • ("sampleurl/param1/param2/2");

  • ("sampleurl/param1/3/2");

  • ("sampleurl/param1/param2/param3");

So when the call goes to API, check there is param1=param1 ignore the value you have to keep 3 values in order to match the signature of API in any case.

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

Comments

5

if your using asp API the parameters should send like this.

  string url = $"https://example.com/api/controllerName/Action?parms1={value}&parms2={value}";
                                    response = client.GetAsync(url).Result;

And if you are using asmx you should edit the web service to handle the parameters like this:

CrudService.getAll("sampleurl",parm1,parm2,etc..);

I hope this will help you.

Comments

3

If you choose such a url structure, it means you have some sort of hierarchy structure. For example mysite.com/vehicle/car/mercedes/classc/details/engine could show or return details of the engine of a class C mercedes. It goes without saying that not transmitting the brand or the car model doesn't make sense.

If you want to keep this hierarchical structure, you must expose an url that permits you to mention the category "undefined" in the structure like: mysite.com/vehicle/car/brandlesscars/mybrandlessmodel/details/engine

If the segments of your url represent a parameter of a query but not a path sequence to access to a more detailed section, it is way better to have a url with parameters like this: mysite.com/vehicle/details/engine?type=car&brand=mercedes&model=classc.

Comments

1

you could explicitly send the request with parameters names and values just like the answer from Paul and applying this to your example: CrudService.getAll("http://sampleurl/controller/action?param1="value"&param2="value&param3=value");

i hope this will help you

Comments

0

The answer of @Jin Thakur is one way to do it but I did this instead. Similar to the answer of @Sinan. Thanks all.

From JS:

CrudService.getAll("/api/home/monitor/param?param1=&param2=&param3=val1");

From C# API:

[Route("api/home/monitor/param")]
[HttpGet]
public IHttpActionResult GetSomeValue(string param1, string param2, string param3)
{

//Some Codes

}

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.