1

I'm rewriting a simple API GET function. I want to make it more flexible, so I tried to copy the example given in this question.

My code:

 public IEnumerable<Distributeur> GetDistributeurs() { NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query); var departementCode = nvc["departementCode"]; // BL comes here var repository = new LeadRepository(); return repository.getDistributeurs(departementCode); } 

Unfortunately I get an error 'cannot resolve RequestUri' and on build:

Error 11 'System.Web.HttpRequestBase' does not contain a definition for 'RequestUri' and no extension method 'RequestUri' accepting a first argument of type 'System.Web.HttpRequestBase' could be found (are you missing a using directive or an assembly reference?)

I went to the Microsoft docs but they are almost empty.

3 Answers 3

7

The Request object is an instance of HttpRequest - not HttpWebRequest. There is no property HttpRequest.RequestUri. The property you are looking for is Request.Url.Query:

HttpUtility.ParseQueryString(Request.Url.Query); 

http://msdn.microsoft.com/en-us/library/system.web.httprequest.url.aspx

Sign up to request clarification or add additional context in comments.

Comments

4

You're confusing System.Net.HttpWebRequest with System.Web.HttpRequest.

The former is used to initiate HTTP requests to foreign servers. The latter is used to represent an HTTP request sent to your server (and is aliased as Request in pages and controllers, referencing System.Web.HttpContext.Current.Request).

Comments

1

I am quite late...But I would like to share my experience to others. I had the same problem. Finally, I found it was because I inherited wrong base class.

API => System.Web.Http.ApiController MVC => System.Web.Mvc.Controller 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.