C# – How to 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.

Best Solution

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: