How to implement a search box in an ASP.NET MVC application?

I need to implement a “Search” field in the C # MVC application that I am writing.

I never had to implement the “Search” window, and I was looking for some best practices, and I don’t quite understand what I’m looking for.

I really like how search works in stackoverflow.

If I type a few random words, it goes to the url http : // stackoverflow / search? Q = few + random + words .

If I type the title: random, it goes to the URL https://stackoverflow.com/search?q=title%3Arandom

What happens both on the client (when I press the enter key) and on the server to perform a search?

I deliberately overlooked any thoughts that I already had about what was happening, because I did not want to bias the answers (or show my ignorance).

EDIT . I add some features of this question.

  • Where and how are search terms converted to querystring parameters? those. several random words converted to several + random + words, name: random conversion to the title% 3Arandom

  • Where and how little + random + words are translated into the variables used in the query?

  • Is the query just one big Where clause that continues to add "and" for each element that fits between the + signs?

I think you could parse the strings and make some replacements to achieve 1 and 2, but there seems to be something already available that automatically converts (and returns) the search strings. I am trying to be prepared for the fact that my user is typing something in the search box.

+4
source share
3 answers

As much as I hate doing this, I have to answer my question. What I could not understand is how search words that seemingly automatically convert to user-encoded parameters (i.e., all spaces that are replaced by a + sign are replaced by% 20). I did not understand how this is achieved, and I like it, so I wanted to have the same abilities.

In the end, what I had to do was copy the html from SO and try it on my own MVC site, because it turns out that the encoding is built-in / automatic. I did not have to do anything to get functionality.

Here is the basic HTML for the search box:

<form id="frmsearch" action="~/Catalog/Search" method="get"> <input id="q" name="q" value="@q" style="width:275px;"/> <input id="submit" name="submit" type="submit" style="font-weight:bold;" value="Search" /> </form> 

Now, if you type “a few random words” into the “q” text box and click the “Submit” button, the form action will automatically lead you to “~ / Catalog / Search? Q = few + random + words” without any additional encoding.

Now for the best part of the controller code, the "q" parameter is automatically available as "a few random words" without additional coding.

Example:

 public ActionResult Search(string q) { //q = "few random words" (no need to remove '+' signs) var model = GetSearchResults(q) return View(model); } 

The only thing I have not tested is how it will handle attacks on scripts, but I think that I will also get it for free. :)

Hope this helps anyone who stumbles upon this answer. Thanks to everyone who provided answers trying to help. I apologize if my question was not clear enough.

+2
source

These URLs use what is called a query string. This is a "GET" request, which allows the client script (javascript), as well as the reverse code to receive the user request. In the url when you see '?' this is the beginning of the query string. This allows someone to be as follows:

http://google.com?q=Stuff%20to%20Search%20here Several parameters can be added via & anothercommand = somethingelse

Thus, allowing a program or script to invoke a google search without having to type anything in a field.

You can access the query string using C # "Request.QueryString [" parameter "]", where in this case the parameter for this URL will be "q".

After that, you query your database and return the results. Since I'm not sure how good at coding, I'm not sure if you are trying to ask for a website or C # SQL side. If I'm wrong, apologize.

On the client: The way I assume this is happening on the client is that the script in the text field when sending the form is redirected to the URL you specified and adds the url string to these request parameters. Remember to encode url. This is built into javascript. those. space '' becomes "% 20"

When the form is submitted, the server code checks to see if there are any query string parameters in the q form. If they are and are not null, it will query the database, returning it in one of several ways, most likely through the server control.

1) This is what URL encoding is. This is a list of characters that are not supported in the URL. Therefore, they need to be changed. There is a standard set, such as% 20 for space. In javascript, you redirect to the results page with the desired query string. before redirecting, use here to encode it. those. change '' to + or% 20 (it really should be% 20, I find +, as a rule, in the way of an Internet explorer.)

2) The query string acts as a hash table of key pair values. Using Request.QueryString, you can select the key "q" and get the string "a few random words." Then it will be replaced with your SQL query. This is done on the C # side as the very first check to see if the q parameter exists.

3) you can make your request in many ways. However, the search for "and", etc. Will give you many different results. What you can do is parse the list of common words, and then rank the results based on the number of results for each word. those. in the most simplified search, which is not recommended for large databases "..... Where, for example,"% word% or "% word2%", etc. To get each word, execute string.split.

+3
source

All Articles