当前位置:网站首页>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 !
边栏推荐
- Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
- PTA year of birth
- ISO 27001 Information Security Management System Certification
- Small exercise -- subnet division and summary
- February 16, 2022 Daily: graph neural network self training method under distribution and migration
- DRF --- response rewrite
- What is web application security testing technology?
- Static timing analysis (STA) in ic/fpga design
- (6) VIM editor MV cat redirection standard input and output more pipe symbols head tail
- Computer network interview assault
猜你喜欢

Mysql database design

Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week

Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion

How to retrieve the password for opening Excel files

t10_ Adapting to Market Participantsand Conditions

Apache iceberg source code analysis: schema evolution

Product service, operation characteristics

People help ant help task platform repair source code

Penetration practice vulnhub range Nemesis

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
随机推荐
Review Net 20th anniversary development and 51aspx growth
Common design parameters of solid rocket motor
Is the fund of futures account safe? How to open an account?
网上股票开户安全吗?是否可靠?
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
Euler function: find the number of numbers less than or equal to N and coprime with n
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
Definition of rotation axis in mujoco
Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
Detailed explanation of select in golang
Irradiance, Joule energy, exercise habits
Opencv map reading test -- error resolution
February 16, 2022 Daily: graph neural network self training method under distribution and migration
Source code of new campus errand / campus task platform on mutual station
聊聊项目经理最爱使用的工具
EasyCVR设备录像出现无法播放现象的问题修复
SCP -i private key usage
Three dimensional anti-terrorism Simulation Drill deduction training system software
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
Equipment simulation and deduction training system software