简体   繁体   English

Selfhost REST / ApiController - 过滤参数怎么做?

[英]Selfhost REST / ApiController - How to do filterparameters?

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.我需要创建一个 REST 服务作为 winforms 应用程序的一部分。 It needs to return json.它需要返回 json。 No html and no other webstuff, so i dont need the ASP.Net/Core overhead.没有 html 也没有其他 webstuff,所以我不需要 ASP.Net/Core 开销。
  • I decided on OWIN selfhost because the Self-Host ASP.NET Web API is tagged as " old, use owin instead ", so i did that.我决定使用 OWIN selfhost,因为 Self-Host ASP.NET Web API 被标记为“旧的,改用owin”,所以我这样做了。
  • It is not ASP MVC它不是 ASP MVC

What works:什么有效:

What i didnt get to work is those ?category=searchterm stuff.我没有开始工作的是那些?category=searchterm的东西。

In the old API https://docs.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.在旧的 API https: //docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/self-host-a-web-api 中,只需添加类似这个,我不知道如何,但它看起来像反射魔法这应该由 /api/products/?category=category 调用,在我的情况下,在 OWIN 中它没有。

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!感谢您花时间阅读!

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.特别感谢@MindSwipe,他的链接不是答案,但让我走上了正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM