0

My asp.net web api is an standalone application,face problem to pass json sa a parameter.My api method is bellow

[Route("api/UniqueUser/{userInfo}")]
        public HttpResponseMessage GetUniqueUserByEmail(string userInfo)
{
}

In above parameter userInfo is a josn like bellow

{"UserInfo":[{"Id":1,"UserName":"Jxj Bdn","Email":"[email protected]"}]}

When I put this in my browser url show me bellow error

enter image description here

1 Answer 1

1

JSON data should go in the body of the request for it to be deserialized, not in the query string/browser URL.

Also, 'string userInfo' will not work as you expect. You can define a class that represents the parameters of your JSON object and it will work correctly.

This would work, for example:

public class UserInfo
{
    public int Id { get; set;}

    public string UserName  { get; set;}

    public string Email  { get; set;}
}

and change this line:

public HttpResponseMessage GetUniqueUserByEmail(UserInfo userInfo)

Edit:

If it's a url that someone needs to pass in you use routing:

https://site/api/UniqueUser/1/Jxj Bdn/[email protected]

And in your controller:

[Route("api/UniqueUser/{id}/{userName}/{email}")]
public HttpResponseMessage GetUniqueUserByEmail(int id, string userName, string email)

Have a look here to see how to do this with traditional query string parameters too:

http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

I would strongly suggest using the first method though, it gives you a strongly type object and is a lot easier to deal with if details change, and you get the benefit of the build in model validation.

Can you not make a simple HTML form for your clients to use?

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

1 Comment

as i say it's a service application,so i need to give user just a url nothing else,in this situation what i do.

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.