Email Address in Web API GET request

2019-02-28 16:42发布

问题:

I'm working on a REST APIs implemented in ASP.NET Web API. One scenario we'd like to support is to do a GET request of some user information by email address.

Ideally, clients should be able to do something like this:

GET: /api/v1/users/email/your_email@your_domain.com

When I attempt to run this GET request, such as through the browser, then I get the 404 Not Found page.

What am I doing wrong that is causing IIS to not run the Web API route handler for my route? If I try requesting /api/v1/users/email/your_email, then it routes correctly.

回答1:

Can you try the following and see if it works for you?

GET: /api/v1/users/email?id=your_email%40your_domain.com

This assumes your controller's Get function is something like this: Get(string id), i.e. it takes a parameter called id of type string. If you want it to take, for example, a parameter called "emailAddress", then you would do:

GET: /api/v1/users/email?emailAddress=your_email%40your_domain.com

Note the @ sign is encoded as %40. This also assumes your controller is called EmailController.



回答2:

I just managed to test it for you.

The main issue is that "IIS Web Core" confused "." from email address with "." from file extension (e.g. .aspx .php and etc)

If you remove "." from email address and replace it with "-" or something then it will work.

Eg.

GET /api/values/me@gmail-com

 public string Get(string id)
        {
            return "value";
        }

Hope it helps.

You may see how to remove the default IIS module but I would recommend to reconsider your API design again.