1

The query string I use is always empty. I have no idea why, and have tried for hours with The HttpContext.Request returns all other parts of the URL except the querystring.

With this url https://localhost:44394/Trackers/Create?Place=Vision_College

and this Model

 [BindProperties(SupportsGet = true)] 
    public partial class Tracker
    {  
     [FromQuery(Name = "Place")]  //populates it from the query 
      public string Place { get; set; }
     ...}

and this controller

 [HttpPost]
 [ValidateAntiForgeryToken]
 public async Task<IActionResult> Create([Bind("Name, Phone, Place")] Tracker tracker)
        {

Empty Query

2
  • How exactly are you posting this URL? Commented May 15, 2020 at 3:09
  • The URL is coming from a QR code and loads the page correctly. Just the Querystring is not being accepted. It seems that the problem is that the Query string itself held in the the Request is always empty. Its not getting as far as putting it into the Property. Is there something I am missing that goes in the startup? Commented May 16, 2020 at 3:26

2 Answers 2

1

OK I found an answer. I was trying to use it in the POST of the CREATE, when I should have been using it in the GET part of CREATE Using GET to get querystring

Thanks for everyones help!

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

Comments

0

Since you are using the query parameters in httpPost you should use, [FromQuery] inside your arguments. Follow this

Your DTO class would be,

public class Tracker
{
  [FromQuery(Name = "Place")]
  public string Place{ get; set; }
}

In your controller class


[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([FromQuery]Tracker tracker)
{

}

Note: If your query parameters match with the model property names, specifically annotating the properties would not be necessary.

Better you can get by body itself since this is a post request. otherwise make this as a get request. If converting to get by body, simply use [FromBody] in endpoint arguments instead of [FromQquery]

3 Comments

The URL is coming from a QR code and loads the page correctly. Just the Querystring is not being filled. It seems that the problem is that the Query string itself held in the Request is always empty. Its not getting as far as putting it into the Property. Is there something I am missing that goes in the startup?
From where are you accessing HttpContext ? Post the full source code please
It comes from an empty if (HttpContext.Request.Path.HasValue) {} that I have later in the code. Otherwise its just a stock Create Post generated from Scaffolding. I am just trying to get the property Tracker.Place to hold the query string that comes in on the URL. HttpContext allows me to see the entire URL and its components, its not used as such, at this stage. I did try BusinessName = HttpContext.Request.Query["Place"].ToString(); but its empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.