日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET Core快速入门(第4章:ASP.NET Core HTTP介绍)--学习笔记

發布時間:2023/12/4 asp.net 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET Core快速入门(第4章:ASP.NET Core HTTP介绍)--学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊藍字關注我們

課程鏈接:http://video.jessetalk.cn/course/explore

良心課程,大家一起來學習哈!

任務22:課程介紹

  • 1.HTTP 處理過程

  • 2.WebHost 的配置與啟動

  • 3.Middleware 與管道

  • 4.Routing MiddleWare 介紹

任務23:Http請求的處理過程

任務24:WebHost的配置

  • 1.覆蓋配置文件

  • 2.更改啟動URL

  • 3.IHostingEnvironment

  • 4.IApplicationLifetime

  • 5.dotnet watch run

dotnet new web

settings.json

{
"ConnectionStrings":{
"DefaultConnection":"Server=...;Database=...;"
}
}

Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(configureDelegate=>{
configureDelegate.AddJsonFile("settings.json");
})
.UseStartup<Startup>();

Startup.cs

using Microsoft.Extensions.Configuration;

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.Run(async (context) =>
{
// await context.Response.WriteAsync("Hello World!");
// JsonFile
await context.Response.WriteAsync(configuration["ConnectionStrings:DefaultConnection"]);
});
}

啟動HelloCore,輸出結果

Program.cs

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(configureDelegate => {
//configureDelegate.AddJsonFile("settings.json");
configureDelegate.AddCommandLine(args);
})
//.UseUrls("http://localhost:5001")
.UseStartup<Startup>();

Startup.cs

app.Run(async (context) =>
{
// await context.Response.WriteAsync("Hello World!");
// JsonFile
//await context.Response.WriteAsync(configuration["ConnectionStrings:DefaultConnection"]);
// CommandLine
await context.Response.WriteAsync($"name={configuration["name"]}");
});

設置應用程序參數

啟動HelloCore,輸出結果

任務25:IHostEnvironment和 IApplicationLifetime介紹

Startup.cs

app.Run(async (context) =>
{
await context.Response.WriteAsync($"ContentRootPath = {env.ContentRootPath}");
await context.Response.WriteAsync($"EnvironmentName = {env.EnvironmentName}");
await context.Response.WriteAsync($"WebRootPath = {env.WebRootPath}");
});

啟動HelloCore,輸出結果

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IConfiguration configuration, IApplicationLifetime applicationLifetime)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

applicationLifetime.ApplicationStarted.Register(() =>
{
Console.WriteLine("Started");
});

applicationLifetime.ApplicationStopped.Register(() =>
{
Console.WriteLine("Stopped");
});

applicationLifetime.ApplicationStopped.Register(() =>
{
Console.WriteLine("Stopped");
});

app.Run(async (context) =>
{
await context.Response.WriteAsync($"ContentRootPath = {env.ContentRootPath}");
await context.Response.WriteAsync($"EnvironmentName = {env.EnvironmentName}");
await context.Response.WriteAsync($"WebRootPath = {env.WebRootPath}");
});
}

啟動HelloCore,輸出結果

我心中的ASP.NET Core 新核心對象WebHost(一):

http://www.jessetalk.cn/2017/11/11/aspnet-core-object-webhost/#comment-194

我心中的ASP.NET Core 新核心對象WebHost(二):

http://www.jessetalk.cn/2017/11/14/aspnet-core-object-webhost-build/

任務26:dotnet watch run 和attach到進程調試

New Terminal

dotnet new web --name HelloCore

F5 Start Debug

在csproj 的 ItemGroup 添加引用

<DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0" />

New Terminal

dotnet restore
dotnet watch run

修改代碼保存后會自動重啟

瀏覽器刷新即可看到更新結果

attach到進程調試

任務27:Middleware管道介紹

  • 1.Middleware 與管道的概念

  • 2.用 Middleware 來組成管道實踐

  • 3.管道的實現機制(RequestDelegate 與 ApplicationBuilder)

startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// 添加一個中間件,傳一個名字為next的request delegate
app.Use(async (context,next)=>{
await context.Response.WriteAsync("1: before start...");
await next.Invoke();
});

// 接收一個RequestDelegate,返回一個RequestDelegate
app.Use(next=>{
return (context)=>{
context.Response.WriteAsync("2: in the middle of start..");
return next(context);
};
});

app.Run(async (context) =>
{
await context.Response.WriteAsync("3: start...");
});
}

啟動項目,輸出結果

// 如果不調用next,則管道終止,不會輸出"3: start..."
app.Use(next=>{
return (context)=>{
return context.Response.WriteAsync("2: in the middle of start..");
//return next(context);
};
});

// 使用Map構建路由,通過localhost:5000/task訪問
app.Map("/task", taskApp=>{
taskApp.Run(async context=>{
await context.Response.WriteAsync("this is a task");
});
});

// 添加一個中間件,傳一個名字為next的request delegate
app.Use(async (context,next)=>{
await context.Response.WriteAsync("1: before start...");
await next.Invoke();
});

// 接收一個RequestDelegate,返回一個RequestDelegate
// 如果不調用next,則管道終止,不會輸出"3: start..."
app.Use(next=>{
return (context)=>{
context.Response.WriteAsync("2: in the middle of start..");
return next(context);
};
});

app.Run(async (context) =>
{
await context.Response.WriteAsync("3: start...");
});

訪問 https://localhost:5001/task

任務28:RequestDelegate管道實現思路

  • 1.RequestDelegate

  • 2.ApplicationBuilder:多個RequestDelegate拼接

// 添加一個中間件,傳一個名字為next的RequestDelegate
app.Use(async (context,next)=>{
await context.Response.WriteAsync("1: before start...");// 完成自己處理
await next.Invoke();// 調用下一步
});

// 封裝一個function交給ApplicationBuilder處理
app.Use(next=>{
return (context)=>{
context.Response.WriteAsync("2: in the middle of start..");
return next(context);
};
});

任務29:自己動手構建RequestDelegate管道

新建一個控制臺程序

dotnet new console --name MyPipeline

新建一個類RequestDelegate.cs

using System;
using System.Threading.Tasks;

namespace MyPipeline
{
public delegate Task RequestDelegate(Context context);
}

新建一個類Context.cs

using System;
using System.Threading.Tasks;

namespace MyPipeline
{
public class Context
{

}
}
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace MyPipeline
{
class Program
{
public static List<Func<RequestDelegate,RequestDelegate>>
_list = new List<Func<RequestDelegate, RequestDelegate>>();
static void Main(string[] args)
{
Use(next=>{
return context=>{
Console.WriteLine("1");
return next.Invoke(context);
};
});

Use(next=>{
return context=>{
Console.WriteLine("2");
return next.Invoke(context);
};
});

RequestDelegate end = (Context)=>{
Console.WriteLine("end...");
return Task.CompletedTask;
};

_list.Reverse();
foreach(var middleware in _list)
{
end = middleware.Invoke(end);
}

end.Invoke(new Context());
Console.ReadLine();
}

public static void Use(Func<RequestDelegate,RequestDelegate> middleware)
{
_list.Add(middleware);
}
}
}

在任何一個Middleware可以結束管道

Use(next=>{
return context=>{
Console.WriteLine("1");
//return next.Invoke(context);
return Task.CompletedTask;// 結束管道調用,只輸出1
};
});

任務30:RoutingMiddleware介紹以及MVC引入

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace HelloCore
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();// 添加依賴注入配置
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// 通過localhost:5000/action訪問
app.UseRouter(builder=>builder.MapGet("action", async context=>{
await context.Response.WriteAsync("this is a action");
}));

// 使用Map構建路由,通過localhost:5000/task訪問
app.Map("/task", taskApp=>{
taskApp.Run(async context=>{
await context.Response.WriteAsync("this is a task");
});
});

// 添加一個中間件,傳一個名字為next的request delegate
app.Use(async (context,next)=>{
await context.Response.WriteAsync("1: before start...");
await next.Invoke();
});

// 如果不調用next,則管道終止,不會輸出"3: start..."
app.Use(next=>{
return (context)=>{
return context.Response.WriteAsync("2: in the middle of start..");
//return next(context);
};
});

app.Run(async (context) =>
{
await context.Response.WriteAsync("3: start...");
});
}
}
}

訪問 https://localhost:5001/action

使用UseRouter方法2

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace HelloCore
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRouting();// 添加依賴注入配置
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

// 使用UseRouter方法2
// 通過localhost:5000/action訪問
RequestDelegate handler = context=>context.Response.WriteAsync("this is a action");
var route = new Route(
new RouteHandler(handler),
"action",
app.ApplicationServices.GetRequiredService<IInlineConstraintResolver>()
);

app.UseRouter(route);

// 使用UseRouter方法1
// 通過localhost:5000/action訪問
app.UseRouter(builder=>builder.MapGet("action", async context=>{
await context.Response.WriteAsync("this is a action");
}));

// 使用Map構建路由,通過localhost:5000/task訪問
app.Map("/task", taskApp=>{
taskApp.Run(async context=>{
await context.Response.WriteAsync("this is a task");
});
});

// 添加一個中間件,傳一個名字為next的request delegate
app.Use(async (context,next)=>{
await context.Response.WriteAsync("1: before start...");
await next.Invoke();
});

// 如果不調用next,則管道終止,不會輸出"3: start..."
app.Use(next=>{
return (context)=>{
return context.Response.WriteAsync("2: in the middle of start..");
//return next(context);
};
});

app.Run(async (context) =>
{
await context.Response.WriteAsync("3: start...");
});
}
}
}

訪問 https://localhost:5001/action

RountingMiddleware介紹

var routeHandler = new RouteHandler(context=>context.Response.WriteAsync("test"));

var route = new Route(routeHandler);

new RouteMiddleware(route)

RouteMiddleware.Invoke(httpContext)

_route.RouteAsync(context)

routeMatch(RouteContext)

OnRouteMatched(RouteContext)

點“在看”給我一朵小黃花

總結

以上是生活随笔為你收集整理的ASP.NET Core快速入门(第4章:ASP.NET Core HTTP介绍)--学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。