当前位置:网站首页>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 !
边栏推荐
- Cloud computing - make learning easier
- C operator overloads the query table
- [acnoi2022] color ball
- MySQL connection tools
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- Yolov5 practice: teach object detection by hand
- Highly reliable program storage and startup control system based on anti fuse FPGA and QSPI flash
- Euler function: find the number of numbers less than or equal to N and coprime with n
- Static timing analysis (STA) in ic/fpga design
- Win10+vs2019 Community Edition compiling OpenSSL
猜你喜欢
Classpath classpath
Bernoulli distribution (a discrete distribution)
Android development interview was badly hit in 3 years, and now the recruitment technical requirements are so high?
Calculation of intersection of two line segments
Set the style of QT property sheet control
[PHP foundation] realize the connection between PHP and SQL database
Review Net 20th anniversary development and 51aspx growth
Oracle TRUNC function processing date format
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
New 95 community system whole station source code
随机推荐
transform. Forward and vector3 Differences in the use of forward
Computer network interview assault
Depth first search - DFS (burst search)
MFC obtains local IP (used more in network communication)
Wechat applet blind box - docking wechat payment
2022 Heilongjiang latest fire protection facility operator simulation test question bank and answers
LeetCode 148. Sort linked list
Samba basic usage
MES production equipment manufacturing execution system software
[acnoi2022] color ball
PCL learning materials
L'ouverture d'un compte d'actions en ligne est - elle sécurisée? Fiable?
Mysql database design
Is the fund of futures account safe? How to open an account?
Cloud computing - make learning easier
[2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
Bernoulli distribution (a discrete distribution)
主成分之综合竞争力案例分析