当前位置:网站首页>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 !
边栏推荐
- Thinkphp6 - CMS multi wechat management system source code
- ZABBIX alarm execute remote command
- Basic concepts of binary tree
- ISO 27001 Information Security Management System Certification
- 徽商期货是正规期货平台吗?在徽商期货开户安全吗?
- Oracle TRUNC function processing date format
- Batch export all pictures in PPT in one second
- Fix the black screen caused by iPhone system failure
- Distributed task queue: Celery usage record
- What is web application security testing technology?
猜你喜欢

MySQL connection tools

Localization through custom services in the shuttle application

ISO 27001 Information Security Management System Certification

Domestic spot silver should be understood

Thinkphp6 - CMS multi wechat management system source code

Definition of rotation axis in mujoco
![Htt [ripro network disk link detection plug-in] currently supports four common network disks](/img/e4/c06b94243cdbe391b8b2124ff30789.jpg)
Htt [ripro network disk link detection plug-in] currently supports four common network disks

PCL learning materials

Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?

Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
随机推荐
Yolov5 practice: teach object detection by hand
Set the style of QT property sheet control
The ultimate version of the 13th simulation of the single chip microcomputer provincial competition of the Blue Bridge Cup
Glidefast consulting was selected as the elite partner of servicenow in 2022
网上股票开户安全吗?是否可靠?
Detailed explanation of ArrayList expansion
Wechat applet blind box - docking wechat payment
Can hero sports go public against the wind?
Cassette helicopter and alternating electric field magnetic manometer DPC
Flex layout
Apache iceberg source code analysis: schema evolution
Domestic spot silver should be understood
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
主成分之综合竞争力案例分析
Is it safe to open a securities account? Is there any danger
JS how to convert a string with a delimiter into an n-dimensional array
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
EasyCVR设备录像出现无法播放现象的问题修复
Explain in detail the process of realizing Chinese text classification by CNN
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?