Ad hoc queries for Razor Pages

Ronald Rex 1,666 Reputation points
2021-12-08T21:43:15.14+00:00

Hi Friends, I want to run some ad hoc queries in my .Net 5 Razor pages or Components. I was wondering should I put the query logic in my models or somewhere else? I don't know if making linq query calls to a webapi is efficent and I read that I should use methods or functions in my models and use Linq to Entities. Thanks !!!

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2021-12-09T02:03:06.63+00:00

    Hi @Ronald Rex ,

    I want to run some ad hoc queries in my .Net 5 Razor pages or Components. I was wondering should I put the query logic in my models or somewhere else?

    For the Razor pages, it has a backend PageModel class file, for example:

    Index.cshtml : contains the html elements

    156067-image.png

    and the Index.cshtml.cs file is the backend PageModel class file, you can run the ad hoc queries in the OnGet, OnPost or other Page handler, then display the result on the page.

    in the following code, it will use EF core and DbContext (_context) to do the CURD operation. You can change it to your ad hoc queries.

    156112-image.png

    More detailed information, see Introduction to Razor Pages in ASP.NET Core

    For the Razor components, you can run the ad hoc queries in the @code block and use the component's OnInitialized method or other action method. Refer to ASP.NET Core Razor components, ASP.NET Core Blazor data binding and the ASP.NET Core Blazor event handling

    Besides, I suggest you could refer to the official tutorial (note in the sample, it uses the EF core to manage data, it will query or edit data via the _context), then you can clear to know where to run the query statement.

    if making linq query calls to a webapi is efficent and I read that I should use methods or functions in my models and use Linq to Entities.

    Do you mean after getting data from webapi, you want to use LINQ query to filter data? If that is the case, you can do it, but it is better to directly call the WebAPI and get the required value, in this scenario, there is no need to use the linq filter.

    And for the models, I think it should be the PageModel, right? If that is the case, from the above document, we can see that the page model is used to bind data on the page.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful