|
| 1 | +--- |
| 2 | +title: Add search to ASP.NET Core MVC |
| 3 | +titleSuffix: Azure Cognitive Search |
| 4 | +description: In this Azure Cognitive Search tutorial, learn how to add search to an ASP.NET Core (Model-View-Controller) application. |
| 5 | + |
| 6 | +manager: nitinme |
| 7 | +author: HeidiSteen |
| 8 | +ms.author: heidist |
| 9 | +ms.service: cognitive-search |
| 10 | +ms.devlang: csharp |
| 11 | +ms.topic: tutorial |
| 12 | +ms.date: 03/09/2023 |
| 13 | +--- |
| 14 | +# Create a search app in ASP.NET Core |
| 15 | + |
| 16 | +In this tutorial, create a basic ASP.NET Core (Model-View-Controller) app that connects to the hotels-sample-index on your search service for server-side operations. Search can return sorted, filtered, and boosted results. |
| 17 | + |
| 18 | +Sample code this tutorial can be found in the [azure-search-dotnet-samples](https://github.com/Azure-Samples/azure-search-dotnet-samples/tree/main/create-mvc-app) repository on GitHub. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | ++ [Hotel samples index](search-get-started-portal.md) |
| 23 | ++ [Visual Studio](https://visualstudio.microsoft.com/downloads/) |
| 24 | ++ [Azure.Search.Documents NuGet package](https://www.nuget.org/packages/Azure.Search.Documents/) |
| 25 | ++ [Microsoft.Spatial NuGet package](https://www.nuget.org/packages/Microsoft.Spatial/) |
| 26 | + |
| 27 | +To complete this tutorial, you'll need to create the hotels-sample-index on your search service. Make sure the search index name is`hotels-sample-index`, or change the index name in the `HomeController.cs` file. |
| 28 | + |
| 29 | +Your search service must have public network access. For the connection, the app presents a query API key to your fully-qualified search URL. Both the URL and the query API key are specified in an `appsettings.json` file. |
| 30 | + |
| 31 | +## Set up the project |
| 32 | + |
| 33 | +1. Clone or download this sample repository. |
| 34 | + |
| 35 | +1. Extract contents if the download is a zip file. Make sure the files are read-write. |
| 36 | + |
| 37 | +1. Open the solution in Visual Studio. |
| 38 | + |
| 39 | +1. If necessary, update the NuGet packages to get the most recent stable versions. |
| 40 | + |
| 41 | +1. Modify `appsettings.json` to specify your search service and query API key. |
| 42 | + |
| 43 | + ```json |
| 44 | + { |
| 45 | + "SearchServiceName": "https://<YOUR-SEARCH-SERVICE-NAME>.search.windows.net", |
| 46 | + "SearchServiceQueryApiKey": "<YOUR-SEARCH-SERVICE-QUERY-API-KEY>" |
| 47 | + } |
| 48 | + ``` |
| 49 | + |
| 50 | +## Run the sample |
| 51 | + |
| 52 | +1. Press **F5** to compile and run the project. The app runs on local host and opens in your default browser. |
| 53 | + |
| 54 | +1. Select the **Search** button to return all results. |
| 55 | + |
| 56 | +1. This demo uses the default search configuration, supporting the [simple syntax](query-simple-syntax.md) and `searchMode=Any`. You can enter keywords with boolean operators. |
| 57 | + |
| 58 | +1. To change server-side behaviors, modify the **RunQueryAsync** method in the HomeController. There are several use-cases that commented out: |
| 59 | + |
| 60 | + + Use case 1 is basic search results presented in a table. |
| 61 | + + Use case 2 adds a filter over the Category field. |
| 62 | + + Use case 3 adds a sort order over the Rating field. |
| 63 | + |
| 64 | + Recall that the index field attributes determine which fields are searchable, filterable, sortable, facetable, and retrievable. If you want different filters and sort fields, delete and recreate the hotels index with the attribution necessary for your scenario. |
| 65 | + |
| 66 | +1. Relevance tuning is a server-side operation. If you want to boost the relevance of a document based on whether the match was found in a certain field, such as "Tags", or by location, [add a scoring profile](index-add-scoring-profiles.md) to the hotel-search-index, and then rerun your queries. |
| 67 | + |
| 68 | +## Next steps |
| 69 | + |
| 70 | +If you want to explore client-side operations that respond to user actions, consider adding a React template to your solution: |
| 71 | + |
| 72 | +> [!div class="nextstepaction"] |
| 73 | +> [C# Tutorial: Add search to a website with .NET](tutorial-csharp-overview.md) |
0 commit comments