How to get query string parameter value in asp.net core?

14,846

Generally, you should rely on model-binding to access incoming values, not read them explicitly from a certain request source.

However, the correct way to read query-string values is through Request.Query instead. And in your case:

_httpContextAccessor.HttpContext.Request.Query["data"]

See Model-Binding

Share:
14,846

Related videos on Youtube

Răzvan Flavius Panda
Author by

Răzvan Flavius Panda

Updated on June 04, 2022

Comments

  • Răzvan Flavius Panda
    Răzvan Flavius Panda almost 2 years

    I'm trying to get a query string value using:

    _httpContextAccessor.HttpContext.Request.QueryString["data"]
    

    but it fails with error:

    Cannot apply indexing with [] to an expression of type 'QueryString'

    QueryString is from the Microsoft.AspNetCore.Http namespace.

  • Offir
    Offir almost 4 years
    _httpContextAccessor.HttpContext.Request.Query["data"][0]
  • Michael
    Michael over 2 years
    Model-Binding is the ticket, glad you made that point.