• 大小: 7.78MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-02
  • 语言: 其他
  • 标签: webapi  mongodb  C#  

资源简介

webapi + mongodb 实现基础操作(数据的增删查改) 简单实例 代码易懂 直接运行 希望对新人有所帮助

资源截图

代码片段和文件信息

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

namespace ApiClient.Api
{
    public class baseApi where T : classnew()
    {
        private static string url;
        static baseApi()
        {
            url = “person/“;
        }
        public static string AddEntity(T entity string token)
        {
            var uri = Common.baseSetting.Intance.ApiURL + url;
            string content = JsonConvert.Serializeobject(entity);
            var t = HttpMethod.HttpPost.Post(uri content token);
            if (t.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return t.Content.ReadAsString();
            }
            else
            {
                throw new Exception(t.Content.ReadAsString());
            }
        }

        /// 
        /// 根据id获取对象
        /// 

        /// 
        /// 
        /// 
        public static T GetEntity(string id string token)
        {
            var uri = Common.baseSetting.Intance.ApiURL + url + id;

            var t = HttpMethod.HttpGet.Get(uri token);
            if (t.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string s = t.Content.ReadAsString();
                return JsonConvert.Deserializeobject(s);
            }
            else
            {
                throw new Exception(t.Content.ReadAsString());
            }
        }
        public static S GetEntity(Dictionary paramters string token)
        {
            var uri = Common.baseSetting.Intance.ApiURL + url;
            HttpQueryString p = null;
            if (paramters != null&¶mters.Count>0)
            {
                p = new HttpQueryString();
                foreach (var dic in paramters)
                {
                    p.Add(dic.Key dic.Value);
                }
            }
            var t = HttpMethod.HttpGet.Get(uri p token);
            if (t.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string s = t.Content.ReadAsString();
                return JsonConvert.Deserializeobject(s);
            }
            else
            {
                throw new Exception(t.Content.ReadAsString());
            }
        }
        public static string ModifyEntity(string id object entity string token)
        {
            var uri = Common.baseSetting.Intance.ApiURL + url + id;
            string content = JsonConvert.Serializeobject(entity);
            var t = HttpMethod.HttpPut.Put(uri content token);
            if (t.StatusCode == System.Net.HttpStatusCode.OK)
            {
                return t.Content.ReadAsString();
            }
            else
            {
                throw new Exception(t.Content

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      85995  2018-09-17 08:36  WebApi_Mongodb_Pro_demo\.vs\config\applicationhost.config

    ..A..H.    193024  2018-09-21 13:32  WebApi_Mongodb_Pro_demo\.vs\WebApi_Mongodb_Pro\v15\.suo

     文件          0  2018-09-13 09:04  WebApi_Mongodb_Pro_demo\.vs\WebApi_Mongodb_Pro\v15\Server\sqlite3\db.lock

     文件       4096  2018-09-13 09:04  WebApi_Mongodb_Pro_demo\.vs\WebApi_Mongodb_Pro\v15\Server\sqlite3\storage.ide

     文件      32768  2018-09-21 09:12  WebApi_Mongodb_Pro_demo\.vs\WebApi_Mongodb_Pro\v15\Server\sqlite3\storage.ide-shm

     文件    2607992  2018-09-17 11:22  WebApi_Mongodb_Pro_demo\.vs\WebApi_Mongodb_Pro\v15\Server\sqlite3\storage.ide-wal

     文件       4701  2018-09-17 09:12  WebApi_Mongodb_Pro_demo\ApiClient\Api\baseApi.cs

     文件        248  2014-10-28 15:28  WebApi_Mongodb_Pro_demo\ApiClient\Api\PersonApi.cs

     文件       3886  2014-10-28 15:04  WebApi_Mongodb_Pro_demo\ApiClient\ApiClient.csproj

     文件      12800  2014-10-29 10:24  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\ApiClient.dll

     文件      34304  2014-10-29 10:24  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\ApiClient.pdb

     文件     119648  2014-10-28 11:26  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\Microsoft.Http.dll

     文件      28536  2014-10-28 11:26  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\Microsoft.Http.Extensions.dll

     文件     430592  2014-10-28 14:55  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\Newtonsoft.Json.dll

     文件     464518  2014-10-28 14:55  WebApi_Mongodb_Pro_demo\ApiClient\bin\Debug\Newtonsoft.Json.xml

     文件        718  2014-10-28 15:55  WebApi_Mongodb_Pro_demo\ApiClient\Common\baseSetting.cs

     文件       1078  2014-10-29 09:33  WebApi_Mongodb_Pro_demo\ApiClient\Entity\Person.cs

     文件       1462  2014-10-28 14:40  WebApi_Mongodb_Pro_demo\ApiClient\HttpMethod\HttpDelete.cs

     文件       1894  2014-10-28 15:57  WebApi_Mongodb_Pro_demo\ApiClient\HttpMethod\HttpGet.cs

     文件       1784  2014-10-28 16:05  WebApi_Mongodb_Pro_demo\ApiClient\HttpMethod\HttpPost.cs

     文件       2053  2014-10-28 15:04  WebApi_Mongodb_Pro_demo\ApiClient\HttpMethod\HttpPut.cs

     文件     119648  2014-10-28 11:26  WebApi_Mongodb_Pro_demo\ApiClient\Lib\Microsoft.Http.dll

     文件      28536  2014-10-28 11:26  WebApi_Mongodb_Pro_demo\ApiClient\Lib\Microsoft.Http.Extensions.dll

     文件     430592  2014-10-28 14:55  WebApi_Mongodb_Pro_demo\ApiClient\Lib\Newtonsoft.Json.dll

     文件     464518  2014-10-28 14:55  WebApi_Mongodb_Pro_demo\ApiClient\Lib\Newtonsoft.Json.xml

     文件         42  2018-09-17 08:36  WebApi_Mongodb_Pro_demo\ApiClient\obj\Debug\ApiClient.csproj.CoreCompileInputs.cache

     文件        786  2014-10-28 15:28  WebApi_Mongodb_Pro_demo\ApiClient\obj\Debug\ApiClient.csproj.FileListAbsolute.txt

     文件      74663  2018-09-17 08:36  WebApi_Mongodb_Pro_demo\ApiClient\obj\Debug\ApiClient.csprojAssemblyReference.cache

     文件      21997  2014-10-28 15:04  WebApi_Mongodb_Pro_demo\ApiClient\obj\Debug\ApiClient.csprojResolveAssemblyReference.cache

     文件      12800  2014-10-29 10:24  WebApi_Mongodb_Pro_demo\ApiClient\obj\Debug\ApiClient.dll

............此处省略295个文件信息

评论

共有 条评论