L. Sanna
L. Sanna

Reputation: 6552

How do I use Request.RequestUri.Query in .NET? (Error 11 'System.Web.HttpRequestBase' does not contain a definition for 'RequestUri')

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.

Upvotes: 1

Views: 12189

Answers (3)

Eric J
Eric J

Reputation: 11

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

Upvotes: 1

David
David

Reputation: 219027

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).

Upvotes: 4

Ant P
Ant P

Reputation: 25231

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

Upvotes: 7

Related Questions