JJ.
JJ.

Reputation: 9950

Is it possible to pass a Session variable (object) to an API as a parameter?

I have a web page with a search criteria.

Once the user selects what he wants and inputs any keywords to search, he is re-directed to another page which shows the results of his search.

This session object contains all of the information of his search:

var ProjectSearchCriteria = (GBLProjectSearchCriteria) Session[GblConstants.SESSION_PROJECT_SEARCH_CRITERIA];

Is there a way for me to pass this object to an API?

Like so:

[HttpGet]
public List<string> getEpisodes(GBLProjectSearchCriteria psc)
{
     var ProjectSearchResult = new ProjectSearchResultController();
     var GBLProjectSearchResultListData = ProjectSearchResult.GetProjectSearchResultList(psc);
     return (from GBLProjectSearchResult item 
             in GBLProjectSearchResultListData
             select item.Title).ToList();
}

The reason why I want to do this is because the search criteria is massive and it already exists so I don't want the API to have 38032823 parameters.

Is this even possible? How would I do it? Any alternatives?

Upvotes: 0

Views: 578

Answers (2)

Web API binds parameters from either URI, query string, etc. or the request body. If you want to bind from any thing else, especially outside of the request message, you can create your own parameter binding. See this. The blog post creates a parameter binding for type IPrincipal but you can do something similar for `GBLProjectSearchCriteria'.

Upvotes: 2

developerwjk
developerwjk

Reputation: 8659

Have you tried?

getEpisodes((GBLProjectSearchCriteria) Session[GblConstants.SESSION_PROJECT_SEARCH_CRITERIA]);

One obvious question I have is, since you know the datatype already, and are type-casting to it, why are you using var?

var ProjectSearchCriteria = (GBLProjectSearchCriteria) ....

Doesn't this make more sense?

GBLProjectSearchCriteria  ProjectSearchCriteria = (GBLProjectSearchCriteria) ....

Upvotes: 0

Related Questions