基于.net framework4.0框架下winform項目實現寄宿式web api

首先Nuget中下載包:Microsoft.AspNet.WebApi.SelfHost,如下:

注意版本哦,最高版本只能4.0.30506能用。

1.配置路由

public static class WebApiConfig{public static void Register(this HttpSelfHostConfiguration config){// 配置JSON序列化設置config.Formatters.Clear();config.Formatters.Add(new JsonMediaTypeFormatter());config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();// 配置路由//config.MapHttpAttributeRoutes();config.Routes.Clear();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional });}}

路由器不要搞錯了,其實和老版本asp.net 差不多。

2.創建一個控制器

public class ValuesController : ApiController{[HttpGet]public string HelloWorld(){return "Hello?World!";}[HttpGet]public ModelTest Test(){var model = new ModelTest();model.Id = Guid.NewGuid().ToString();model.Name = "Test";return model;}[HttpGet]public List<ModelTest> Test2(){List<ModelTest> modelTests = new List<ModelTest>();for (int i = 0; i < 3; i++){var model = new ModelTest();model.Id = Guid.NewGuid().ToString();model.Name = "Test";modelTests.Add(model);}return modelTests;}}

?創建一個WebServer,以來加載實現單例

public class WebServer{private static Lazy<WebServer> _lazy = new Lazy<WebServer>(() => new WebServer());private ManualResetEvent _webEvent;private WebServer(){}public static WebServer Instance => _lazy.Value;public string BaseAddress { get;set; }public Action<WebServer> StartSuccessfulCallback { get; set; }public Action<WebServer> RunEndCallback { get; set; }public Action<WebServer, AggregateException> StartExceptionCallback { get;set; }public void StartWebServer(){if (string.IsNullOrEmpty(BaseAddress)) return;_webEvent=new ManualResetEvent(false);Task.Factory.StartNew(() =>{HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(BaseAddress);config.Register();using (HttpSelfHostServer server = new HttpSelfHostServer(config)){try{server.OpenAsync().Wait();if (StartSuccessfulCallback != null)StartSuccessfulCallback(this);_webEvent.WaitOne();if (RunEndCallback != null)RunEndCallback(this);}catch (AggregateException ex){_webEvent.Set();_webEvent.Close();_webEvent = null;if (StartExceptionCallback != null)StartExceptionCallback(this,ex);}finally{server.CloseAsync().Wait();server.Dispose();}}});}public void StopWebServer(){if (_webEvent == null) return;_webEvent.Set();_webEvent.Close();}}

public class WebApiFactory{static string baseAddress = "http://localhost:9000/";static WebApiFactory(){Server = WebServer.Instance;Server.BaseAddress = baseAddress;}public static WebServer Server { get;private set; }}

?使用

public partial class Form1 : Form{public Form1(){InitializeComponent();WebApiFactory.Server.StartSuccessfulCallback = (t) =>{label1.Text = "Web API hosted on " + t.BaseAddress;};WebApiFactory.Server.RunEndCallback = (t) =>{label1.Text = "Web API End on " + t.BaseAddress;};WebApiFactory.Server.StartExceptionCallback = (t,ex) =>{MessageBox.Show(string.Join(";", ex.InnerExceptions.Select(x => x.Message)));};}private void button1_Click(object sender, EventArgs e){WebApiFactory.Server.StartWebServer();}private void button2_Click(object sender, EventArgs e){WebApiFactory.Server.StopWebServer();}private void Form1_FormClosing(object sender, FormClosingEventArgs e){WebApiFactory.Server.StopWebServer();}}

注:啟動時必須以管理員身份啟動程序

?我們掛的是http://localhost:9000/,接下來我們去請求:http://localhost:9000/api/Values/Test2

擴展:簡單添加權限驗證,不通過路由

public class BasicAuthorizationHandler : DelegatingHandler{protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken){if (request.Method == HttpMethod.Options){var optRes =  base.SendAsync(request, cancellationToken);return optRes;}if (!ValidateRequest(request)){var response = new HttpResponseMessage(HttpStatusCode.Forbidden);var content = new Result{success = false,errs = new[] { "服務端拒絕訪問:你沒有權限" }};response.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");var tsc = new TaskCompletionSource<HttpResponseMessage>();tsc.SetResult(response); return tsc.Task;}var res =  base.SendAsync(request, cancellationToken);return res;}/// <summary>/// 驗證信息解密并對比/// </summary>/// <param name="message"></param>/// <returns></returns>private bool ValidateRequest(HttpRequestMessage message){var authorization = message.Headers.Authorization;//如果此header為空或不是basic方式則返回未授權if (authorization != null && authorization.Scheme == "Basic" && authorization.Parameter != null){string Parameter = authorization.Parameter;// 按理說發送過來的做了加密,這里需要解密return Parameter == "111";// 身份驗證碼}else{return false;}}}/// <summary>/// 構建用于返回錯誤信息的對象/// </summary>public class Result{public bool success { get; set; }public string[] errs { get; set; }}

然后在WebApiConfig中注冊

// 注冊身份驗證
config.MessageHandlers.Add(new BasicAuthorizationHandler());

根據自己需求做擴展吧,這里由于時間問題簡單做身份驗證(全局)

根據控制器或方法添加身份驗證(非全局):

public class AuthorizationAttribute : AuthorizationFilterAttribute{public override void OnAuthorization(HttpActionContext actionContext){// 如果驗證失敗,返回未授權的響應if (!IsUserAuthorized(actionContext)){// 如果身份驗證失敗,返回未授權的響應var content = new Result{success = false,errs = new[] { "服務端拒絕訪問:你沒有權限" }};actionContext.Response= actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Unauthorized");actionContext.Response.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");}}private bool IsUserAuthorized(HttpActionContext actionContext){var authorizationHeader = actionContext.Request.Headers.Authorization;if (authorizationHeader != null && authorizationHeader.Scheme == "Bearer" && authorizationHeader.Parameter != null){// 根據實際需求,進行適當的身份驗證邏輯// 比較 authorizationHeader.Parameter 和預期的授權參數值return authorizationHeader.Parameter == "111";}return false;}}
public static class WebApiConfig{public static void Register(this HttpSelfHostConfiguration config){// 注冊身份驗證(全局)//config.MessageHandlers.Add(new BasicAuthorizationHandler());config.Filters.Add(new AuthorizationAttribute());// 配置JSON序列化設置config.RegisterJsonFormatter();// 配置路由config.RegisterRoutes();}private static void RegisterJsonFormatter(this HttpSelfHostConfiguration config){config.Formatters.Clear();config.Formatters.Add(new JsonMediaTypeFormatter());config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();}private static void RegisterRoutes(this HttpSelfHostConfiguration config){//config.MapHttpAttributeRoutes();config.Routes.Clear();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional });}}

然后在控制器中:

 public class ValuesController : ApiController{[HttpGet]public string HelloWorld(){return "Hello?World!";}[HttpGet]public ModelTest Test(){var model = new ModelTest();model.Id = Guid.NewGuid().ToString();model.Name = "Test";return model;}[Authorization][HttpGet]public List<ModelTest> Test2(){List<ModelTest> modelTests = new List<ModelTest>();for (int i = 0; i < 3; i++){var model = new ModelTest();model.Id = Guid.NewGuid().ToString();model.Name = "Test";modelTests.Add(model);}return modelTests;}}

全局異常處理:

 public class GlobalExceptionFilter : IExceptionFilter{public bool AllowMultiple => false;public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken){// 在這里實現自定義的異常處理邏輯// 根據實際需求,處理異常并生成適當的響應// 示例:將異常信息記錄到日志中LogException(actionExecutedContext.Exception);// 示例:返回帶有錯誤信息的響應var content = new Result{success = false,errs = new[] { "發生了一個錯誤" }};actionExecutedContext.Response = actionExecutedContext.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Internal Server Error");actionExecutedContext.Response.Content = new StringContent(JsonConvert.SerializeObject(content), Encoding.UTF8, "application/json");var tcs = new TaskCompletionSource<object>();tcs.SetResult(null);return tcs.Task;}private void LogException(Exception exception){// 在這里編寫將異常信息記錄到日志的邏輯}}
public static class WebApiConfig{public static void Register(this HttpSelfHostConfiguration config){// 注冊身份驗證(全局)//config.MessageHandlers.Add(new BasicAuthorizationHandler());config.Filters.Add(new AuthorizationAttribute());// 注冊全局異常過濾器config.Filters.Add(new GlobalExceptionFilter());// 配置JSON序列化設置config.RegisterJsonFormatter();// 配置路由config.RegisterRoutes();}private static void RegisterJsonFormatter(this HttpSelfHostConfiguration config){config.Formatters.Clear();config.Formatters.Add(new JsonMediaTypeFormatter());config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();}private static void RegisterRoutes(this HttpSelfHostConfiguration config){//config.MapHttpAttributeRoutes();config.Routes.Clear();config.Routes.MapHttpRoute(name: "DefaultApi",routeTemplate: "api/{controller}/{action}/{id}",defaults: new { id = RouteParameter.Optional });}}

寫一個測試接口:

        [HttpGet]public int Test3(){int a = 3;int b = 0;return a / b;}

我們知道有5個Filter,這里只用到了其中的兩個,其它自定義實現

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/162904.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/162904.shtml
英文地址,請注明出處:http://en.pswp.cn/news/162904.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Axure插件瀏覽器一鍵安裝:輕松享受高效工作!

Axure插件對原型設計師很熟悉&#xff0c;但由于Axure插件是在國外開發的&#xff0c;所以在安裝Axure插件時不僅需要下載中文包&#xff0c;激活步驟也比較繁瑣&#xff0c;有時Axure插件與計算機系統不匹配&#xff0c;Axure插件格式不兼容。本文將詳細介紹如何安裝Axure插件…

uniapp開發小程序-pc端小程序下載文件

fileName包含文件名后綴名&#xff0c;比如test.png這種格式 api.DownloadTmtFile后端接口返回的是文件的二進制流 值得注意的是&#xff0c;微信開發者工具中是測試不了wx.saveFileToDisk的&#xff0c;需要真機或者體驗版測試 handleDownload(fileName) {if (!fileName) retu…

CCFCSP試題編號:201912-2試題名稱:回收站選址

這題只要比較坐標的四周&#xff0c;然后計數就可以了。 #include <iostream> using namespace std;int main() {int n;cin >> n;int arr[1005][2] { 0 };int res[5] { 0 };int up 0;int down 0;int left 0;int right 0;int score 0;for (int i 0; i <…

QT 在Windows下實現ping功能(ICMP)

前言 很多時候&#xff0c;我們可能會圖省事直接調用系統中的ping命令&#xff0c;但這是很不科學的~ 廢話不多說&#xff0c;直接上代碼.. .pro文件 在.pro文件末尾添加一行&#xff1a; LIBS -liphlpapi -lws2_32 .h文件 在.h文件中加入&#xff1a; #include <Q…

23款奔馳GLC260L升級原廠360全景影像 高清環繞

本次星駿匯小許介紹的是23款奔馳GLC260L升級原廠360全景影像&#xff0c;上帝視角看清車輛周圍環境&#xff0c;更輕松駕駛 升級360全景影像系統共有前后左右4個攝像頭&#xff0c;分別在車頭&#xff0c;車尾&#xff0c;以及兩邊反光鏡下各一個&#xff0c;分別用來采集車頭&…

C# 宏--釋義及實例

1.宏-釋義 在C#中&#xff0c;宏&#xff08;Macro&#xff09;通常指的是預處理指令&#xff08;Preprocessor Directive&#xff09;&#xff0c;用于在編譯時對源代碼進行一些宏替換或條件編譯的操作。C#中的宏使用預處理器指令#define和#undef來定義和取消定義宏&#xff…

C++string_view簡介

1. 簡介 C17之后才有string_view&#xff0c;主要為了解決C語言常量字符串在std::string中的拷貝問題。 即readonly的string。 2. 引入 2.1 隱式拷貝問題 將C常量字符串拷貝了一次 #include <iostream> #include <string>int main() {std::string s{ "He…

Modbus RTU、Modbus 庫函數

Modbus RTU 與 Modbus TCP 的區別 一般在工業場景中&#xff0c;使用 Modbus RTU 的場景更多一些&#xff0c;Modbus RTU 基于串行協議進行收發數據&#xff0c;包括 RS232/485 等工業總線協議。采用主從問答式&#xff08;master / slave&#xff09;通信。 與 Modbus TCP 不…

基于springboot實現實習管理系統的設計與實現項目【項目源碼+論文說明】計算機畢業設計

基于sprinmgboot實現實習管理系統的設計與實現演示 摘要 隨著信息化時代的到來&#xff0c;管理系統都趨向于智能化、系統化&#xff0c;實習管理也不例外&#xff0c;但目前國內仍都使用人工管理&#xff0c;市場規模越來越大&#xff0c;同時信息量也越來越龐大&#xff0c;…

普通平衡樹

題意&#xff1a;略&#xff0c;題中較清晰。 用二叉查找樹來存儲數據&#xff0c;為了增加效率&#xff0c;盡量使左子樹和右子樹的深度差不超過一&#xff0c;這樣可以時間控制在logn&#xff0c;效率比較高。 右旋和左旋&#xff0c;目的是為了維護二叉樹的操作&#xff0…

Dubbo引入Zookeeper等注冊中心簡介以及DubboAdmin簡要介紹,為后續詳解Dubbo各種注冊中心做鋪墊!

文章目錄 一&#xff1a;Dubbo注冊中心引言 1&#xff1a;什么是Dubbo的注冊中心&#xff1f; 2&#xff1a;注冊中心關系圖解 3&#xff1a;引入注冊中心服務執行流程 4&#xff1a;Dubbo注冊中心好處 5&#xff1a;注冊中心核心作用 二&#xff1a;注冊中心實現方案 …

Springboot+vue的新冠病毒密接者跟蹤系統(有報告)。Javaee項目,springboot vue前后端分離項目

演示視頻&#xff1a; Springbootvue的新冠病毒密接者跟蹤系統(有報告)。Javaee項目&#xff0c;springboot vue前后端分離項目 項目介紹&#xff1a; 本文設計了一個基于Springbootvue的新冠病毒密接者跟蹤系統&#xff0c;采用M&#xff08;model&#xff09;V&#xff08;v…

HttpClient實現 get、post、put、delete請求【轉】

來自&#xff1a;HttpClient實現 get、post、put、delete請求_httpclient put請求-CSDN博客 目錄 HttpClient HttpClient的主要功能 httpclient使用示例主要步驟 Spring Boot 工程結構 HttpClient實現主要代碼&#xff1a; GET POST PUT Delete HttpClient HttpCli…

信息系統項目管理師-干系人管理論文提綱

快速導航 1.信息系統項目管理師-項目整合管理 2.信息系統項目管理師-項目范圍管理 3.信息系統項目管理師-項目進度管理 4.信息系統項目管理師-項目成本管理 5.信息系統項目管理師-項目質量管理 6.信息系統項目管理師-項目資源管理 7.信息系統項目管理師-項目溝通管理 8.信息系…

景區智慧旅游智能化系統方案:PPT全文58頁,附下載

關鍵詞&#xff1a;智慧景區解決方案&#xff0c;智慧文旅解決方案&#xff0c;智慧旅游解決方案&#xff0c;智慧文旅綜合運營平臺 一、景區智慧旅游智能化系統建設背景 近年來&#xff0c;隨著信息技術的快速發展和普及&#xff0c;以及旅游市場的不斷擴大和升級&#xff0…

電腦自動刪除文件怎么辦?如何恢復?

在數字化時代&#xff0c;電腦已經成為人們不可或缺的工具之一。然而&#xff0c;由于各種原因&#xff0c;我們有時會遇到電腦自動刪除文件的情況&#xff0c;這給我們的工作和生活帶來了很多不便。那么&#xff0c;當電腦自動刪除文件時&#xff0c;我們應該如何處理呢&#…

【Python爬蟲】8大模塊md文檔從0到scrapy高手,第8篇:反爬與反反爬和驗證碼處理

本文主要學習一下關于爬蟲的相關前置知識和一些理論性的知識&#xff0c;通過本文我們能夠知道什么是爬蟲&#xff0c;都有那些分類&#xff0c;爬蟲能干什么等&#xff0c;同時還會站在爬蟲的角度復習一下http協議。 Python爬蟲和Scrapy全套筆記直接地址&#xff1a; 請移步這…

數據結構與算法編程題14

設計一個算法&#xff0c;通過一趟遍歷在單鏈表中確定值最大的結點。 #include <iostream> using namespace std;typedef int Elemtype; #define ERROR 0; #define OK 1;typedef struct LNode {Elemtype data; //結點保存的數據struct LNode* next; //結構體指針…

RedHat NTP時間服務器配置Chrony(所有節點時間跟主節點時間同步)

NTP NTP&#xff08;Network Time Protocol&#xff09;是一種用于在計算機網絡中同步時鐘的協議。它的主要目的是確保網絡中的各個設備具有準確的時間參考&#xff0c;以便協調事件順序、安全通信和日志記錄等應用。它通過分層體系結構、時間同步算法和準確的時間參考源來確保…

Linux設置靜態IP

Linux設置靜態IP 使用ip addr查看ip&#xff0c;如下所示就是動態IP 1、什么是靜態IP&#xff1f; 靜態ip就是固定的ip&#xff0c;需要手動設置。靜態IP地址&#xff08;又稱固定IP地址&#xff09;是長期分配給一臺計算機或網絡設備使用的 IP 地址。一般來說&#xff0c;一…