public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return WebHost.CreateDefaultBuilder(args).UseStartup<Startup>(); }
Action<object> action = //函數參數 (object obj) => //函數體 { Console.WriteLine("123456"); }; public void action <T>(object obj) { Console.WriteLine("123456"); } public void test() { Action<object> action = new Action<object>(action<object>); action(new object()); //執行 }
//我的自定義委托 public delegate Task Request_Delegate(HttpContext context); //我的自定義函數 public Task My_Function(HttpContext context) { return context.Response.WriteAsync("1213465656"); }
/* 寫法1 */ app.Run( async (context) => { await context.Response.WriteAsync("Hello World!_001"); } ); /* 寫法 2 */ test(app); app.Run( (context) // 類似於匿名函數 => { return context.Response.WriteAsync("Hello World!"); }
#region --定義帶參數的委托 //定義一個委托 public delegate TResult My_Func001<in T1, in T2, out TResult>(T1 arg1, T2 arg2); public delegate string My_Func0011<in T1, in T2, out TResult>(T1 arg1, T2 arg2); public string my_func001<T1,T2>(int a,int b) { MessageBox.Show("0"); return ""; } /* My_Func001<int, int,string> my_Func001 = my_func001<int, int>; my_Func001(1,2); My_Func0011<int, int, string> my_Func001 = my_func001<int, int>; my_Func001(1, 2); */ public delegate void My_Func002<in T1, in T2>(T1 arg1, T2 arg2); public void my_func002<T1,T2>(T1 a,T2 b){} /* My_Func002<int,int> my_Func002 = my_func002<int, int>; my_Func002(10,11); */ #endregion #region --定義無參數的委托 public delegate void My_Func003<in T1, in T2>(); //無參數 public void my_func003() { MessageBox.Show("003"); } public void my_func003<T>() { var t = typeof(T); MessageBox.Show("003<T>" + t.Name); } public void my_func003<T1, T2>() { var t = typeof(T1); MessageBox.Show("003<T1,T2>" + t.Name); } /* 調用 //聲明委托 My_Func003<int, int> my_Func003 = my_func003; My_Func003<int, int> my_Func003_1 = my_func003<String>; My_Func003<int, int> my_Func003_12 = my_func003<String, String>; //執行委托 my_Func003(); my_Func003_1(); my_Func003_12(); */ #endregion
//研究中e......
namespace WebApplication1.lib { //定義一個接口 public interface My_IApplicationBuilder { My_IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware); } public static class A { // public delegate TResult Func<in T, out TResult>(T arg); public static T test<T>(T a) { T temp = default(T); return temp; } public static My_IApplicationBuilder testc(this My_IApplicationBuilder app, string str) { Func<RequestDelegate, RequestDelegate> middleware = test<RequestDelegate>; //執行這個函數 //return app.Use(middleware); //函數當參數傳遞過去 //委托賦值方式 2 Func<RequestDelegate, RequestDelegate> middleware1 = delegate (RequestDelegate next) { //返回方法001 MethodInfo[] array = new MethodInfo[] { }; MethodInfo methodInfo = array[0]; return (RequestDelegate)new object(); }; //匿名方法 return app.Use(middleware1); //函數當參數傳遞過去 } } }
gg了
.net core 如何獲取 每次請求URL 比如 http://localhost:62830/h http://localhost:62830/a.txt http://localhost:62830/a.jpg http://localhost:62830/a http://localhost:62830/b
服務器如何獲取 每次的請求連接?????
//app.Run(My_Function); // 放在 app.UseMvc 之后,將捕獲不到控制器頁面 捕獲所有請求
////我的自定義函數 //public async Task My_Function( HttpContext context) //{ // //RequestHandleHelper request = new RequestHandleHelper(context.Request); // //var t = ; //請求路徑 // var path = context.Request.Path.Value; // var met = context.Request.Method; // //包含的文件擴展名 // if (path=="1") // { // //context.Response.Redirect(path); // //await context.Response.WriteAsync("Hello, World!"); // } // else if(path== "/123") // { // context.Response.Redirect("/Home/Error"); // } //}
//其他委托
//定義一個接口 public interface My_IApplicationBuilder { My_IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware); } public static class A { // public delegate TResult Func<in T, out TResult>(T arg); public static T test<T>(T a) { T temp = default(T); return temp; } public static My_IApplicationBuilder testc(this My_IApplicationBuilder app, string str) { Func<RequestDelegate, RequestDelegate> middleware = test<RequestDelegate>; //執行這個函數 //return app.Use(middleware); //函數當參數傳遞過去 //委托賦值方式 2 Func<RequestDelegate, RequestDelegate> middleware1 = delegate (RequestDelegate next) { //返回方法001 MethodInfo[] array = new MethodInfo[] { }; MethodInfo methodInfo = array[0]; return (RequestDelegate)new object(); }; //匿名方法 return app.Use(middleware1); //函數當參數傳遞過去 } }