基于.NET Core 框架搭建WebApi项目

一 什么是.NET Core?

    随着2014年 Xamarin和微软发起.NET基金会,微软在2014年11月份 开放.NET框架源代码。在.NET开源基金会的统一规划下诞生了.NET Core 。也就是说.NET Core Framework是参考.NET Framework重新开发的.NET实现,Mono是.NET Framework的一个开源的、跨平台的实现。

二 .NET Core与.NET FrameWork和Mono之间的联系?

    .NET Framework将成为.NET Core在Windows上的一个发行版,Mono将成为.NET Core的一个跨平台发行版。

三. 如何构建.NET Core项目?

 1.安装环境配置:

    首先你电脑上的vs版本是2015 并且保证已经升级至 update3及以上,如果你还没升级,请从此链接下载升级 ed2k://|file|cn_visual_studio_enterprise_2015_with_update_3_x86_x64_dvd_8923298.iso|7787208704|A1C1D2AFBC09D8778C92CF19DEC8F4F4|/

    如果你上述操作已经完成,那么你还需要下载.NET Core的SDK和Tools来安装,下载链接https://download.microsoft.com/download/A/3/8/A38489F3-9777-41DD-83F8-2CBDFAB2520C/packages/DotNetCore.1.0.0-SDK.Preview2-x64.exe

https://download.microsoft.com/download/2/F/8/2F864C4E-6980-4AFC-B64E-0AC04837FD6C/DotNetCore.1.0.0-VS2015Tools.Preview2.0.1.exe

提示:如果在安装DotNetCore.1.0.0-VS2015Tools出现以下问题

解决办法如下:

    1.关闭当前的VS开发工具,以便继续安装;

    2.检查是否出现网络问题,去掉IE浏览器的证书认证

  3.如果以上不能帮你解决问题,可以尝试直接进入exe所在的文件夹在运行命令
DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1

2.创建一个.NET Core web项目

我们来看看与传统的MVC有何不同

可以发现与传统的MVC目录结构并没多大出入,那么尝试写一个Demo吧!

新建一个webapi项目:

可以看到路由自定义部分现已放在了APIController中,默认为api/controller,此处已重写路由

如要创建POST或者GET请求函数,与之前定义一致,在函数上面标记上[HttpPost]或者[HttpGet],模拟请求代码就不一一列举了,请自行脑补。

附上一个HttpClient请求帮助类:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    /// <summary>
    /// HttpClient的帮助类
    /// 2015年11月12日14:43:45
    /// </summary>
    public class HttpClientHelper
    {
        /*Edit Desc: 提取默认的Http Heads UserAgen Author:Xuja Time:2016-5-3 16:46:29*/
        private static readonly string userAgen = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36";

        /// <summary>
        /// 根据Url地址Get请求返回数据
        /// 2015年11月12日14:50:02
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <param name="ch_httpcode">http状态码</param>
        /// <returns>字符串</returns>
        public static string GetResponse(string url, out string httpStatusCode)
        {
            httpStatusCode = string.Empty;
            HttpClient httpClient = new HttpClient(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip });
            HttpResponseMessage response = null;
            try
            {
                //httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", userAgen);
                //httpClient = new HttpClient();
                //using (HttpClient httpClient = new HttpClient())
                //{
    
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(
                   new MediaTypeWithQualityHeaderValue("application/json"));
                Task<HttpResponseMessage> taskResponse = httpClient.GetAsync(url);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (HttpResponseMessage response = taskResponse.Result)
                //{
    

                //HttpResponseMessage response = httpClient.GetAsync(url).Result;
                if (response.IsSuccessStatusCode)
                {
                    Task<System.IO.Stream> taskStream = response.Content.ReadAsStreamAsync();
                    taskStream.Wait();
                    //此处会抛出异常:不支持超时设置,对返回结果没有影响
                    System.IO.Stream dataStream = taskStream.Result;
                    System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
                    string result = reader.ReadToEnd();

                    return result;
                }
                //}
                return null;
                //}
            }
            catch
            {
                return null;
            }
            finally
            {
                if (response != null)
                {
                    response.Dispose();
                }
                if (httpClient != null)
                {
                    httpClient.Dispose();
                }
            }
        }

        /// <summary>
        /// 根据Url地址Get请求返回数据
        /// xuja
        /// 2015年11月12日14:50:02
        /// </summary>
        /// <param name="url">请求的地址</param>
        /// <returns>字符串</returns>
        public static string GetResponse(string url)
        {
            HttpClient httpClient = new HttpClient(new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip });
            HttpResponseMessage response = null;
            try
            {
                //httpClient.MaxResponseContentBufferSize = 256000;
                httpClient.DefaultRequestHeaders.Add("user-agent", userAgen);
                //httpClient = new HttpClient();
                //using (HttpClient httpClient = new HttpClient())
                //{
    
                httpClient.CancelPendingRequests();
                httpClient.DefaultRequestHeaders.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(
                   new MediaTypeWithQualityHeaderValue("application/json"));
                Task<HttpResponseMessage> taskResponse = httpClient.GetAsync(url);
                taskResponse.Wait();
                response = taskResponse.Result;
                //using (
  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值