当前位置: 移动技术网 > IT编程>开发语言>.net > .net core 应用Nancy快速实现轻量级webapi

.net core 应用Nancy快速实现轻量级webapi

2019年01月10日  | 移动技术网IT编程  | 我要评论

逐月传说txt,亚伯风尘,凯迪电力怎么样

目前大量数据接口均采用api的方式为各类应用提供数据服务。nancy是.net下实现webapi的一个轻量级的框架,可以快速搭建一个api服务环境,是一种快速建立api服务的不错选择。

本文记录.net core环境下利用nancy快速搭建webapi的全过程。

ⅰ.开发环境

跨平台的: 

宇宙级ide:vs2017

ⅱ.代码实现

1.新建应用框架

 

 

2.下载安装nancy类库,由于需要支持.netcore环境,则需要安装nancy2.0版本。执行下面的包安装命令。

 所需要包的目录结构如下:

 

3.实现.netcore支持nancy,修改startup.cs文件中configure的内容

using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.extensions.dependencyinjection;
using nancy.owin;

namespace nancy.web
{
    public class startup
    {
        // this method gets called by the runtime. use this method to add services to the container.
        // for more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkid=398940
        public void configureservices(iservicecollection services)
        {
        }

        // this method gets called by the runtime. use this method to configure the http request pipeline.
        public void configure(iapplicationbuilder app, ihostingenvironment env)
        {
            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }
            app.useowin(x => x.usenancy());

            //app.run(async (context) =>
            //{
            //    await context.response.writeasync("hello world!");
            //});
        }
    }
}

3.实现路由访问,新建homemodule.cs类,继承nancymodule,开始写nancy格式的路由。路由写法参见文档

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using nancy;
using nancy.modelbinding;

namespace nancy.web
{
    public class homemodule:nancymodule
    {
        public homemodule()
        {
            get("/", r => "hello,nancy,i am running on asp.net core");
            get("/{name}", r => "你好:" + r.name);

post("/loadstr", r => { var strrecive = this.bind<inputstr>(); return strrecive.inputstr; }); } } }

4.解决跨域访问,新建bootstrapper.cs类,该类为nancy特有配置类,重写applicationstartup方法。

using nancy.bootstrapper;
using nancy.tinyioc;

namespace nancy.web
{
    public class bootstrapper: defaultnancybootstrapper
    {
        protected override void applicationstartup(tinyioccontainer container, ipipelines pipelines)
        {
            //cors enable  解决跨域问题           
            pipelines.afterrequest.additemtoendofpipeline((ctx) =>
            {

                ctx.response.withheader("access-control-allow-origin", "*")  // * 允许跨域问题的网站  *号代表面向所有网站  也可指定网站,如 http://localhost:8080
                            .withheader("access-control-allow-methods", "post,get,put,delete,option")
                            .withheader("access-control-allow-headers", "accept, origin, content-type");

            });

        }
    }
}

5.新建inputstr.cs类,用于测试post提交数据

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;

namespace nancy.web
{
    public class inputstr
    {
        public string inputstr { get; set; }//定义输入字符串
    }
}

 

整体文件目录如下:

 

测试一下,看看运行效果

 

 

ⅲ.应用部署

1.发布应用

 

 

2.部署至iis,将上面发布至publish文件夹的文件拷贝到iis服务器上

 

编辑应用程序池,因为是.net core项目,所以.net framework版本设置为“无托管代码”

 

3.利用postman进行程序测试

get方式

post方式

 

好啦,从搭建、测试到部署完成了一个轻量级的webapi。下一步可以根据具体需求进行扩展,愉快的开发接口了。

下载源码戳这里

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网