Skip to content

ithinkso117/MiniWebApi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MiniWebApi

MiniWebApi is a lightweight WebAPI framework for .NET which can help you build a system with WebAPI function quickly.

Features

  • Support WebAPI protocol without anyother ASP.NET part.
  • Build-in http server.
  • Support [FromUrl] and [FromBody]
  • Use Emit to improve performance and reduce memory usage.
  • Signton pattern for request handler, each request will be handled by the same handler, and they are thread-safe.
  • Attributes support, almost the same as the MVC WebAPI。

Code sample

Step 1. Define the handler for handling the WebAPI request.

    public class TestObject
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }
    
    [WebApiHandler("Test")]
    public class TestHandler:BaseHandler
    {

        [Get("GetMyObject")]
        public void GetObject(WebApiHttpContext context)
        {
            context.Response.Write(new TestObject(){Name = "Justin", Age = 10});
        }

        [Get]
        public void Hello1(WebApiHttpContext context, string name, int age)
        {
            context.Response.Write($"Hello {name}, your age is {age}");
        }

        [Get]
        public void Hello2(WebApiHttpContext context, [FromUrl]TestObject obj)
        {
            context.Response.Write($"Hello {obj.Name}, your age is {obj.Age}");
        }

        [Post]
        public void Hello3(WebApiHttpContext context, string name, int age)
        {
            context.Response.Write($"Hello {name}, your age is {age}");
        }

        [Post]
        public void Hello4(WebApiHttpContext context, [FromBody]TestObject obj)
        {
            context.Response.Write($"Hello {obj.Name}, your age is {obj.Age}");
        }
    }

Step 2. Start the server which contains your handlers.

        var server = new WebApiServer("api");
        server.Start(8090);

Contact mail: ithinkso117@163.com

[@Justin]

About

A mini WebApi framework without MVC dependency

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages