

My personal preference is to work with a NameValueCollection which a querystring really is, but using reflection: You can do this without all the overhead of redirection (which is not inconsiderable). You may need to add a using to make use of the NameValueCollection class. NameValues.Remove("temp") // can't remove via indexer NameValues = "hello, world!" // overwrite "temp"

NameValues = "hello!" // add "temp" if it didn't exist Once you have that append it to the URL and redirect.Īlternately, you can add a new key, or modify an existing one, using the indexer: Finally, calling ToString() on the name NameValueCollection returns the name value pairs in a name1=value1&name2=value2 querystring ready format. You can also use the Add method to add a new one, or Remove to remove a value.
Serialize iqueryable to json string format update#
You can then use the Set method to update a value. The ParseQueryString method returns a NameValueCollection (actually it really returns a HttpValueCollection which encodes the results, as I mention in an answer to another question). Response.Redirect(url + updatedQueryString) String updatedQueryString = "?" + nameValues.ToString() Var nameValues = HttpUtility.ParseQueryString(()) You will need to get the values, modify them, then put them back together. You can't modify the QueryString directly as it is readonly. Is there a builtin way to do this, or do I need to do all of the string parsing/recombining manually? In this case to generate a link to the next page of results, I want to change page=1 to page=2 while leaving the rest of the query string unchanged. Search.aspx?q=123&source=WebSearch&page=1&Theme=Blue There may be other parameters in the url used by other components and the value I'm trying to replace may or may not already be defined: Where it gets complicated is that I want to preserve the input query string from the original page for every parameter except the one that I'm trying to change. I can do this simply by append &page=1 or &page=2 etc. Now within the rendering of that page, I want to display a set of links that allow the user to jump to different pages within the search results. This would display the first page of search results. I hope this helps.Ĭhange Single URL query string value I have an ASP.NET page which takes a number of parameters in the query string: It does appear to leave out the "nextPage" link, but that can be accounted for. If I attempt to set it after the expand parameters it returns the default 100, regardless of the value I set top at. If I set the top value before the expand parameters, it works. Apparently the order I set the parameters matters. If I set it to 50, it will return 50 etc. If I set this value to 25, it will return 25. This is all a practice project that I am just using to teach myself a few concepts.The page size in my API controller is set to 100 but you'll notice in the parameters variable I am setting a top value. WebResponse response = request.GetResponse() WebRequest request = WebRequest.Create(url) String url = serviceUri + entity + parameters + expandOptions String parameters = "?$top=100&$count=true" String expandOptions = "&$expand=APPLICATION, APPLICATIONS_SUBSYSTEMS, FILE" In my client, I am building a request and capturing the response: You'll have to forgive me, I am very new at this and learning as I go, so I hope this actually helps. It looks like the order of parameters matters. I was facing the same thing, but I was able to get it working. Public IQueryable Get(ODataQueryOptions queryOptions) I tried the following but it doesn't work properly when using Expand clauses. What I want is for there to be a maximum of 100 and a default of 10. I know I can set the page size like this:Īnd this will only return 10 results, however I still want the user to be able to specify their own $top and $skip values for paging (and deciding how many results they want per page). This then attempts to retrieve all customers, which I don't want. It'll prevent them from retrieving them as it's higher than 100. Luckily this setting means if they do a request like this: There are a lot of Customer entries and I don't want the client requesting too many at once as the query will take a very long time. Setting a default page size for a WebAPI 2 OData service - asp.net I have an ODataController with an endpoint like this:Īnd this setting in the Register method of the WebApiConfig:Ĭonfig.Count().Filter().OrderBy().Expand().Select().MaxTop(100)
