Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin get query string parameters

Tags:

.net

owin

katana

I am trying to get query string parameters from Owin request. Get operation for parameter 'test' remains empty, although this parameter was in query string. How can I read request parameter from OWIN host?

Call:

localhost:5000/?test=firsttest

Code:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseHandlerAsync((req, res) =>
        {
            string paramTest = req.Get<string>("test");                             
            return res.WriteAsync(paramTest);
        });
    }
like image 926
FrenkyB Avatar asked May 04 '16 10:05

FrenkyB


2 Answers

Get<T> looks in the OWIN environment dictionary for any key. The individual GET request parameters aren't part of that dictionary though. You could get the complete query string using req.QueryString which is equivalent to req.Get<string>("owin.RequestQueryString") and returns test=firsttest in your case. That could be easily parsed.

Another option would be something like this:

        app.Use(async (ctx, next) =>
        {
            var param = ctx.Request.Query.Get("test");
            await next();
        });

IOwinRequest implementations provide you with a parsed query string. Note that the object one gets from IOwinContext.Request implements IOwinRequest while the object that is passed to UseHandlerAsync is of a completely different type (Owin.Types.OwinRequest) which neither provides the context nor the parsed query string (afaik).

like image 161
jasper Avatar answered Nov 15 '22 12:11

jasper


Had the same question, here's my solution.

The ApiController that serves as a base class for specific controllers as in this very simple example ( https://learn.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api ) has a property this.Request.RequestUri.Query which contains the query string.

like image 25
Otterprinz Avatar answered Nov 15 '22 12:11

Otterprinz