当前位置:网站首页>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 !
边栏推荐
- Mujoco XML modeling
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- Pytorch crossentropyloss learning
- Kernel stray cat stray dog pet adoption platform H5 source code
- [beauty detection artifact] come on, please show your unique skill (is this beauty worthy of the audience?)
- Apk signature process introduction [easy to understand]
- Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
- L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
- Fix the black screen caused by iPhone system failure
- Win10+vs2019 Community Edition compiling OpenSSL
猜你喜欢
Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
Calculation of intersection of two line segments
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
Operation of cmake under win
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
Samba basic usage
PCL learning materials
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
主成分计算权重
Length of learning and changing
随机推荐
Pytorch crossentropyloss learning
Htt [ripro network disk link detection plug-in] currently supports four common network disks
ArrayList扩容详解
Yolov5 practice: teach object detection by hand
Length of learning and changing
To improve the efficiency of office collaboration, trackup may be the best choice
Software construction scheme of smart factory collaborative management and control application system
Key points on February 15, 2022
When the fixed frequency artifact falls in love with multithreading | ros2 fixed frequency topic release demo
Talk about the favorite tools used by project managers
ISO 27001 Information Security Management System Certification
Subnet division and summary
Bernoulli distribution (a discrete distribution)
Step size of ode45 and reltol abstol
Thinkphp6 - CMS multi wechat management system source code
Is online stock account opening safe? Is it reliable?
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
Debiasing word embeddings | talking about word embedding and deviation removal # yyds dry goods inventory #
Flex layout
期货账户的资金安全吗?怎么开户?