Isley
Isley

Reputation: 197

ServiceStack pass base.Request.Querystring as a parameter to another class

I have implemented ServiceStack pagination, partial response, filtering etc. I put all the reusable methods into a RequestUtilities class.

And I have this base.Request.Querystring in almost all my methods. (have to access to the request to retrieve attributes, keys, values etc stuff).

Now I need to access to it from my GET() within my XServices.cs. I assume I will need to pass base as a parameter of methods to achieve this. But so far, I couldn't find a way to do it. What is the type of this base? How can I pass it as a parameter? Please advise. Thanks a lot.

from RequestUtilities.cs, such as string qValue = base.Request.QueryString["q"].ToString();

to be used in XServices.cs, such as:

int count = base.Request.QueryString.Count;

PS: how can I decorate my snippet with highlighted class name etc on Stack Overflow. I used Ctrl + k but names, methods etc are not highlighted.

Upvotes: 1

Views: 229

Answers (1)

mythz
mythz

Reputation: 143389

The base.Request just refers to any class that inherits from a class that has IRequest property, i.e:

IRequest Request { get; }

Primarily it refers to your Service base class, but other built-in ServiceStack classes like Razor Views as well as the base ASP.NET Page or ServiceStack Controller classes which also provide access to the current IRequest.

The IRequest is just like any other instance variable where you'll need to pass it in to your utility methods and helper classes (i.e. from your Service) in order for them to have access to it. If they're just static helper methods than it's nicer to use Extension methods so you can call them from your Service with:

var result = base.Request.MyCustomHelper(arg1);

See HttpRequestExtensions.cs for examples of IRequest extension methods.

If you're on an ASP.NET Host you can also access it via the singleton:

HostContext.GetCurrentRequest();

But this isn't recommended since it's Host dependent.

Upvotes: 2

Related Questions