0

I got multiple problems. The first is that i dont know what many things are called because its a "you never did this before and it needs to be done yesterday"-project.

  • I need to create a REST service as part of a winforms application. It needs to return json. No html and no other webstuff, so i dont need the ASP.Net/Core overhead.
  • I decided on OWIN selfhost because the Self-Host ASP.NET Web API is tagged as "old, use owin instead", so i did that.
  • It is not ASP MVC

What works:

What i didnt get to work is those ?category=searchterm stuff.

In the old API https://learn.microsoft.com/en-us/aspnet/web-api/overview/older-versions/self-host-a-web-api it is shown to work by just adding a method like this and i don't know how but it looks like by reflection magic this should be called by /api/products/?category=category which in my case, in OWIN it does not.

public IEnumerable<Product> GetProductsByCategory(string category)
{
  return products.Where(p => string.Equals(p.Category, category,StringComparison.OrdinalIgnoreCase));
 }

I'm not sure what i should do. The documentation doesn't show anything on how to enable it. Depending on where i looks, its called "query" or "filter", but searching that leads to a lot of stuff that is not related. What is that even called?

Thanks for taking the time to read!

Otterprinz
  • 459
  • 3
  • 10
  • Those `?category=searchterm` are called [query string](https://en.wikipedia.org/wiki/Query_string) and a quick Google search yields [this](https://stackoverflow.com/q/37025797/9363973) SO Q&A. But I would highly suggest you actually use [ASP.NET Core API](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio) instead of raw OWIN – MindSwipe Jun 07 '21 at 13:31
  • `GetProductsByCategory([FromQuery] string category)` – Chris Pickford Jun 07 '21 at 15:00
  • @MindSwipe I don't understand that. So a normal Get is done by having an ApiController class that is reflection-parsed and a Get with a query string is done completely differently as some app config async handler? Maybe its the language barrier, but i have no idea how to apply that Q&A answer to my problem. – Otterprinz Jun 07 '21 at 15:04
  • @ChrisPickford The [FromQuery] attribute does not exist in the System.Web.Http namespace like the [FromBody] etc. do. Is that supposed to work for OWIN or is it ASP.Net? – Otterprinz Jun 07 '21 at 15:09

1 Answers1

0

Surprise, the answer is damn simple.

    public IEnumerable<string> Get()
    {
        // The ApiController has a property for the current request which contains the query string.
        var query = this.Request.RequestUri.Query;
        return new string[] { "value1", "value2" };
    }

Special thanks to @MindSwipe whose link wasn't the answer but got me on the right track.

Otterprinz
  • 459
  • 3
  • 10