Getting started for ASP.NET Core + MVC or Razor pages

Here we describe how to add EasyQuery to your ASP.NET Core application with MVC or Razor Pages views.

EasyQuery samples

The easiest way to start getting acquainted with EasyQuery framework is to install and launch our demo projects available on GitHub. You can clone that repository with samples, download it as a ZIP archive or even right away!

Prerequisites

  1. You have an ASP.NET Core version 2.1+ project with MVC views (or Razor pages)
  2. Your application uses Entity Framework Core to work with the database. (NB: it's possible to use EasyQuery without EF Core as well but for the sake of simplicity we will not consider other cases here).

Step 0: Register your trial version

To start using EasyQuery trial you need to register and get your trial version keys first. It takes 1-2 minutes of your time. On registration you will get access to a special "Client's Area" of our web-site where you can see available licenses, get the keys, and submit a support request if necessary.

Step 1: Add EasyQuery packages to your project

First, we are going to add EasyQuery to your ASP.NET Core backend project.

Since we have a standard ASP.NET Core project with EntityFramework Core - then we will need the following 3 EasyQuery packages be added to your .csproj file:

    <PackageReference Include="Korzh.EasyQuery.AspNetCore" Version="5.3.0" />
    <PackageReference Include="Korzh.EasyQuery.EntityFrameworkCore.Relational" Version="5.3.0" />
    <PackageReference Include="Korzh.EasyQuery.RazorUI" Version="5.3.0" />

The latest package is a Razor UI library which contains several ready-to-use page templates (partial views) that work with EasyQuery components.

Step 2: Add EasyQuery services and middleware

The next step will be to setup EasyQuery services and the middleware. Here are the parts your will need to add to your Startup class:

public void ConfigureServices(IServiceCollection services)
{
    .    .    .    .   .   .
    services.AddEasyQuery()
        .UseSqlManager();
}

Here we've added the service and set the default EasyQuery manager (the main "engine" which will process all request). Currently there two possible options: EasyQueryManagerSql and EasyQueryManagerLinq. In the code above we set the first one as default.

Then we need to setup the middleware which will handle HTTP requests from the client-side, pass them to manager, take the result and prepare it for sending back to the client. To avoid any conflicts with MVC we put EasyQuery middleware before it:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
       .   .   .   .   .   .
    app.UseEasyQuery(options => {
        options.Endpoint = "/api/easyquery";
        options.UseDbContext<AppDbContext>();
        options.UsePaging(25);
    });
    app.UseMvc();
}

The code above defines the following main settings:

  1. The endpoint is set to /api/easyquery. This means that all requests from EasyQuery widgets will be started with this path (like /api/easyquery/models/{modelId} to load the model). Please note that this is the default value so you can omit this setting in your app.
  2. We will use the DbContext class specified in UseDbContext call to get the model or to execute the generated queries. It's the fasteset way to specify the model load and the connection to your DB. Otherwise you will need to set them separately via UseModelLoader or UseDbConnection extension functions.
  3. The last UsePaging call turns on the paging and set the page size.

So, server-side setup is finished. Now we need to setup the client-side part.

Step 3: Add EasyQuery page

There are 3 possible options to define the view:

Option 1: Use one of the predefined pages from EasyQuery Razor UI library

So, to simplify the task of rendering an advanced search or data-filtering views EasyQuery includes a Razor UI library package (Korzh.EasyQuery.RazorUI) that contains several predefined views which you can use on your pages. In the simplest case you can just add Korzh.EasyQuery.RazorUI package to your project and run your project. If everything is set up with the defaults, then right out-of-the-box you get access to an "advanced search" page via /EasyQuery/AdvancedSearch.

Option 2: Use some partial view from the Razor UI library

Of course you may need to modify some view options or change the page styles. In this case you can use the partial view accessible via EasyQuery/_AdvancedSearch.
All you need to do for that:

  1. Reference Korzh.EasyQuery.RazorUI package in your project
  2. Create a new page where you plan to provide an advanced search or adhoc reporting functionality. Let's suppose this new page will be accessible as /CustomSearch
  3. Add the styles necessary to render EasyQuery components into Styles section:
@section Styles {
    <link rel="stylesheet" href="https://cdn.korzh.com/eq/6.0.0/eq.core.min.css">
    <link rel="stylesheet" href="https://cdn.korzh.com/eq/6.0.0/eq.view.min.css">
}

(don't forget to add @RenderSection("Styles", false); command to your _Layout.cshtml if you don't have such section yet)

  1. Add the following tag which includes an _AdvancedSearch partial view somewhere to your new page:
<partial name="EasyQuery/_AdvancedSearch" />
  1. Add Scripts section to your page with all necessary libraries ( eq.all.min.js) and the code which will initalize the advanced search view (more about EasyQuery views) on your page:
@section Scripts {
    <!-- EasyQuery script -->
	<script src="https://cdn.korzh.com/eq/6.0.0/eq.enterprise.min.js"></script>

    <!-- EasyQuery view initialization -->
    <script>
        document.addEventListener('load', () => {
            //Options for AdvancedSearchView
            var options = {
                //the base URL where all requests are sent to 
				endpoint: '/api/easyquery',
                widgets: {
		                entitiesPanel: {
                        //EntityiesPanel options
				                showCheckboxes: true
		                },
		                columnsPanel: {
                        //ColumnsPanel options
		                },
		                queryPanel: {
                        //Querypanel options
		                },
                },
                result: {
                    //Don't show EasyChart
                    showChart: false,
                },
            };
						
            var view = new easyquery.ui.AdvancedSearchView();
			window['AdvancedSearchView'] = view; //to make possible to access it later
			
			//The following line is necessary when you use Enterprise edition of EasyQuery.JS
			//You don't need it if you included the Community (free) edition.
			//You can get the trial license key at  https://korzh.com/easyquery#get-started
			view.getContext().useEnterprise("YourTrialLicenseKey");
            view.init(options);
        });
    </script>
}

Option 3: Define your own page

  1. Add your own view or a Razor page (let's suppose it will be accessible by /CustomSearch path.

  2. Reserve several "slots" (empty div elements) for EasyQuery widgets. Each slot must have a special ID to allow EasyQuery to find it:

    • EntitiesPanel - to show the content of your data model
    • ColumnsPanel - to show the panel which allow to edit the list of query column
    • QueryPanel - for the widget which allows to add and modify query conditions
    • ResultPanel - the panel where the query results will be rendered
    • StatementPanel - add it if you want to show the generated SQL statetement for your users
  3. Add initialization script as it was described in Option 2 above.

With this approach you can create almost any possible layout and styling of your "advanced search / ad hoc reporting" page. Please, contact us if you have any questions.

Done!

Now if you run your project and open /CustomSearch page (or /EasyQuery/AdvancedSearch if used the first option) - you will see the default EasyQuery page for advanced database search (like this one)