当前位置:网站首页>C# SelfHost WebAPI (2)
C# SelfHost WebAPI (2)
2022-07-01 18:33:00 【flysh05】
Last autonomous hosting Web Api Examples of store some problems , as follows Self Host Web Api test OK.
1. establish ConsoleApp
Define project name : SefHostWebApiSample
2. Add third party components
Microsoft.AspNet.WebApi.SelfHost
Microsoft.Net.Http
Newtonsoft.Json
3. establish Controller class ,ValuesController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace SefHostWebApiSample
{
public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2", "value3" }; } // GET api/values/5 public string Get(int id) { return "value" +id; } // POST api/values public void Post([FromBody] string value) { } // PUT api/values/5 public void Put(int id,[FromBody] string value) { } // DELETE api/values/5 public void Delete(int id) { } }
}
4. Program.cs Add SelfHost Code
using System.Web.Http.SelfHost;
using System.Web.Http;
namespace SefHostWebApiSample
{
class Program { static void Main(string[] args) { Uri myUri = new Uri(@"http://localhost:9999"); // Let have our configurations readY HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(myUri); // configure routes config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); HttpSelfHostServer server = new HttpSelfHostServer(config); // Start listening server.OpenAsync().Wait(); Console.WriteLine("WebApi hosted on " + myUri.AbsoluteUri + " It can be tested now"); Console.ReadLine(); } }
}
5. function App
App Display the following information :
WebApi hosted on http://localhost:9999/ It can be tested now
Type in the browser :http://localhost:9999/api/values
This XML file does not appear to have any style information associated with it. The document tree is shown below.
value1
value2
value3
Type in the browser :http://localhost:9999/api/values/5
This XML file does not appear to have any style information associated with it. The document tree is shown
value5
test OK !
边栏推荐
- Operation of cmake under win
- Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
- A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
- Computer network interview assault
- An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press
- Relationship between sensor size, pixel, dpi resolution, inch and millimeter
- The latest intelligent factory MES management system software solution
- Growing up in the competition -- (Guangyou's most handsome cub) Pikachu walking
- SCP -i private key usage
- Draw drawing process of UI drawing process
猜你喜欢
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
PTA year of birth
Good looking UI mall source code has been scanned, no back door, no encryption
Quick foundation of group theory (5): generators, Kelley graphs, orbits, cyclic graphs, and "dimensions" of groups?
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Extract the compressed package file and retrieve the password
Static timing analysis (STA) in ic/fpga design
LeetCode 148. Sort linked list
ISO 27001 Information Security Management System Certification
Database - MySQL advanced SQL statement (I)
随机推荐
Data query language (DQL)
[beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
Blue Bridge Cup real topic: the shortest circuit
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
ISO 27001 Information Security Management System Certification
Happy new year | 202112 monthly summary
Penetration practice vulnhub range Nemesis
Apache iceberg source code analysis: schema evolution
Length of learning and changing
Session layer of csframework, server and client (1)
必看,时间序列分析
What are the six steps of the software development process? How to draw software development flow chart?
聊聊项目经理最爱使用的工具
Thinkphp6 - CMS multi wechat management system source code
This is the latest opportunity of the London bank trend
How to learn automated testing?
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
[CF1476F]Lanterns
Explain in detail the process of realizing Chinese text classification by CNN
Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash