ASP.NET Core 101 - Data in a Razor Page [5 of 13]
We have a class whose job it is,is to get that data,and if we did it right,our webpage won't even know whether it's a database or a web service or file.
If you were following along, just a reminder that you can go up to github.com/dotnet-presentations,we're working on the Contoso Crafts workspace.
Now we've got to figure out which page is actually get to use the service.We've got an index.chtml and we had changed that early on.
cshtml
That cshtml is C# HTML.It's a really interesting syntax where it's kind of HTML and it's kind of C#.
It's interesting because you can type in HTML and just do as usual. But then as soon as you hit an at(@) sign , and then there's C#. It lets you do server-side thing that you normally have to create.
Let's notice that this has a directive at the top.It's just at(@)page.Hey, this is a page. Then it says at model. Index and index model.
@page
@model IndexModel
What is index model?
This is a page and there's another file,it's like the friend of this page, it's a buddy(朋友; 同伴; ) page. That's the code behind it, what's called the page model.
using ContosoCrafts.WebSite.Models; using ContosoCrafts.WebSite.Services; namespace ContosoCrafts.WebSite.Pages { public class IndexModel : PageModel { private readonly ILogger<IndexModel> _logger; public IndexModel(ILogger<IndexModel> logger, JsonFileProductService productService) { _logger = logger; ProductService = productService; } public JsonFileProductService ProductService { get; } public IEnumerable<Product> Products { get; private set; } public void OnGet() { Products = ProductService.GetProducts(); } } }
logger
The first thing we notice is that there's like a logger. What's a logger? Well logging is a service that is made available to you by ASP.NET.
private readonly ILogger<IndexModel> _logger; public IndexModel(ILogger<IndexModel> logger) { _logger = logger; }
You don't make a logger,you ask for one, and you ask for one by simply listing it in your arguments.
ProductService
what are some other services that I might want? Maybe a JSON file products service.
public JsonFileProductService ProductService { get; } public IndexModel(ILogger<IndexModel> logger, JsonFileProductService productService) { _logger = logger;
ProductService = productService; }
Remember before we told it about the service,and now anyone who asks for it will get one.
I can make all kinds of services,do all kinds of different stuff,do this and services do that,and anyone can ask for one by just putting in their constructor.
all this stuff that you maybe want to reuse in the future. That you want to reuse.I appreciate(欣赏; 赏识; 重视; ) that you said that because it's all about reuse and this whole thing has been about making code that I can use in multiple places.
Products
The index page,as we had said before,should list out products.So we need to go retrieve those products in that service. We said that they were IEnumerable.
public IEnumerable<Product> Products { get; private set; }
Now we're telling about models.So we have a newer product and then let's call this Products, plural (复数).
private set
it's a private set.Only this class can set them.
OnGet
Razor pages have an idea of an on something,on get, on post, on put. Those are all words that you hear in the HTTP world.
public void OnGet() { Products = ProductService.GetProducts(); }
We will say ProductService,which is this variable here.This model, the model associated(有关联的; 相关的; 有联系的; ) with this page now knows about products.
cshtml Model
@foreach (var product in Model.Products) { <h2>@product.Title</h2>
}
Model is a special word, Model.Products.(循环里面product也需要@符号)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步