基础知识:什么是ASP.NET Razor页面? ASP.NET Core 和 Entity Framework Core

Razor页面与ASP.NET MVC开发使用的视图组件非常相似,它们具有所有相同的语法和功能。

最关键的区别是模型和控制器代码也包含在Razor页面中。它更像是一个MVVM(Model-View-ViewModel)框架,它支持双向数据绑定,更简单的开发体验,具有独立的关注点。

您可以认为Razor页面是WebForms的演变。

https://www.cnblogs.com/tdfblog/p/asp-net-razor-pages-vs-mvc.html

使用依赖注入注册上下文

using ContosoUniversity.Data;
using Microsoft.EntityFrameworkCore;
public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<SchoolContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddMvc();
}

打开appsettings.json文件并添加连接字符串

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=ContosoUniversity1;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  }
}

 ASP.NET Core Razor 页面路由,更改默认的根文件夹

public void ConfigureServices(IServiceCollection services)
{ 
 services 
  .AddMvc(). 
  AddRazorPagesOptions(options => { 
   options.RootDirectory = "/Content";
  }); 
}

 如果您使用的是PageModel,那么是这样实现的:

using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering;
 
namespace RazorPages.Pages
{
 public class ProductModel : PageModel
 {
  public int Id { get; set; }
  public void OnGet(int id)
  {
   Id = id;
  }
 }
}
@page "{id}"
@model ProductModel
<p>The Id is @Model.Id</p>
public void ConfigureServices(IServiceCollection services)
{
 services
  .AddMvc()
  .AddRazorPagesOptions(options =>
  {
   options.Conventions.AddPageRoute("/extras/products", "product");
  });
}
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync($@"{env.WebRootPath}/errors/500.html");

 支持的操作系统

https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/index?view=aspnetcore-2.1&tabs=aspnetcore2x#common-errors

IIS部署问题 HTTP Error 502.5 - Process Failure
下载.net framework 4.6.1安装包

已禁用 CoreWebEngine 或 W3SVC 服务器功能

(W3SVC 服务器功能)找到World Wide Web Publishing Service服务项是禁用

安装ASP.NET Core 模块

https://www.microsoft.com/net/download

安装.NET SDK

https://www.microsoft.com/net/download/dotnet-core/2.0

测试是否安装成功

基础知识:什么是ASP.NET Razor页面?
ASP.NET Core 和 Entity Framework Core