当前位置:网站首页>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 !
边栏推荐
- What are the six steps of the software development process? How to draw software development flow chart?
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- The method of real-time tracking the current price of London Silver
- Xia CaoJun ffmpeg 4.3 audio and video foundation to engineering application
- Mujoco's biped robot Darwin model
- Work and leisure suggestions of old programmers
- Wechat applet blind box - docking wechat payment
- 传感器尺寸、像素、DPI分辨率、英寸、毫米的关系
- Penetration practice vulnhub range Nemesis
- Rust language - cargo, crates io
猜你喜欢
Database - MySQL advanced SQL statement (I)
Domestic spot silver should be understood
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
Check log4j problems using stain analysis
Mujoco model learning record
Review Net 20th anniversary development and 51aspx growth
What impact will multinational encryption regulation bring to the market in 2022
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
. Net cloud native architect training camp (permission system code implements actionaccess) -- learning notes
What are the legal risks of NFT brought by stars such as curry and O'Neill?
随机推荐
Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
Definition of rotation axis in mujoco
On the language internationalization of Yongzhong Office
Mujoco model learning record
Mujoco XML modeling
Length of learning and changing
Penetration practice vulnhub range Keyring
Data query language (DQL)
[PHP foundation] realize the connection between PHP and SQL database
How to learn automated testing?
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
網上股票開戶安全嗎?是否可靠?
Irradiance, Joule energy, exercise habits
Yolov5 practice: teach object detection by hand
Record 3 - the state machine realizes key control and measures the number of external pulses
期货账户的资金安全吗?怎么开户?
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
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
Common design parameters of solid rocket motor