您的位置:68399皇家赌场 > 虚拟主机 > Web API与公事操作

Web API与公事操作

发布时间:2019-07-22 04:38编辑:虚拟主机浏览(160)

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Insus.NET.Models
    {
        public class File
        {
            public int Afd_nbr { get; set; }
    
            public byte[] Picture { get; set; }
    
            public string PictureType { get; set; }
    
            public string FileExtension { get; set; }
        }
    }
    

    ASP.NET MVC的调节器中,创立2个Action:
    澳门皇家赌场55533网址 1

     

     

    Source Code

     
    接下去,大家得以设计Web API接口,待完毕了,发布至英特网,其余客户端就足以操作了。

    Source Code

     

    澳门皇家赌场55533网址 2

    澳门皇家赌场55533网址 3澳门皇家赌场55533网址 4

    收纳你供给写调控器了,不,大家仿佛少写了一些物件,如model和Entity:

    上边Insus.NET尝试写一下。使用最简便易行的措施,就是循环每一笔记录。然后能够测算qty加上前一笔的eqty。

    在视图中,能够那样做:
    澳门皇家赌场55533网址 5

    澳门皇家赌场55533网址 6澳门皇家赌场55533网址 7

    另一个调控器:
    澳门皇家赌场55533网址 8

    有四个网络基友问及,在SQL中,总括每一笔的多余数。他提供的截图评释:
    澳门皇家赌场55533网址 9

    Source Code

    澳门皇家赌场55533网址 10澳门皇家赌场55533网址 11

    接下去,大家须求创制三个认证过滤器:
    澳门皇家赌场55533网址 12

    管理结果:
    澳门皇家赌场55533网址 13

    澳门皇家赌场55533网址 14澳门皇家赌场55533网址 15

    写到这里,开掘少了三个仓储进程,正是获得某一张图纸的:
    澳门皇家赌场55533网址 16

    Source Code

    澳门皇家赌场55533网址,接下去,Insus.NET写了二个仓库储存进程,方法如下:
    澳门皇家赌场55533网址 17

     

     public ActionResult ShowPhoto()
            {
                return View();
            }
    
            public ActionResult ShowPicture(int id)
            {
                var files = ApiUtility.Get<Insus.NET.Models.File>("http://localhost:9001/api/file/"   id);
                var model = files.FirstOrDefault();
    
                PictureResult pictureResult = new PictureResult(model.Picture, model.PictureType);
                return pictureResult;
            }
    

     

    CREATE TABLE #tt
    (
      [empid] char(3),
      [fdate] date,
      [qty] int
    )
    
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-01',100)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-01',100)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-01',120)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-01',145)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',30)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',150)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',160)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',170)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',121)
    INSERT INTO #tt([empid],[fdate],[qty]) VALUES('100','2018-01-02',106)
    

    澳门皇家赌场55533网址 18澳门皇家赌场55533网址 19

    澳门皇家赌场55533网址 20澳门皇家赌场55533网址 21

    后天Insus.NET有在数据库完毕过对某一字段进展加密码与解密《运用EncryptByPass可可沙滩se和DecryptByPass班达沙滩se对MS SQLServer某一字段时行加密和平消除密》[

     

    Source Code

    澳门皇家赌场55533网址 22澳门皇家赌场55533网址 23

     

    CREATE TABLE #ttt
    (
        [id] int identity(1,1),
        [empid] char(3),
        [fdate] date,
        [qty] int, 
        [eqty] int
    )
    
    INSERT INTO #ttt([empid],[fdate],[qty]) SELECT  [empid],[fdate],[qty]  FROM #tt
    

     

    接下去,大家在调控器创制视图的Action:
    澳门皇家赌场55533网址 24

     

    澳门皇家赌场55533网址 25澳门皇家赌场55533网址 26

    程序运营:
    澳门皇家赌场55533网址 27

     

    澳门皇家赌场55533网址 28澳门皇家赌场55533网址 29

    Source Code

     

    using Insus.NET.Entities;
    using Insus.NET.ExtendMethods;
    using Insus.NET.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    
    namespace Insus.NET.Controllers
    {
        public class FileController : ApiController
        {
            // GET: File
            FileEntity fe = new FileEntity();
    
            // GET: ApiFileDemo
    
            [HttpGet]
            public string Get(int id)
            {
                File f = new File();
                f.Afd_nbr = id;
                return fe.GetFileByPrimarykey(f).ToJson();
            }
    
            [HttpPost]
            public void Post(File f)
            {
                fe.Insert(f);
            }
    
            [HttpPut]
            public void Put(File f)
            {
                fe.Update(f);
            }
    
            [HttpDelete]
            public void Delete(File f)
            {
                fe.Delete(f);
            }
    
            [HttpDelete]
            public void Delete(int id)
            {
                File f = new File();
                f.Afd_nbr = id;
                fe.Delete(f);
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace Insus.NET.Utilities
    {
       public class SecurityBase
        {
            public static bool IsAuthorized
            {
                get
                {
                    return CookieBase.Cookie == null ? false : bool.Parse(CookieBase.Cookie.Values["IsAuthorized"]);
                }
                set
                {
                    HttpCookie httpCookie = CookieBase.Cookie == null ? CookieBase.NewCookie : CookieBase.Cookie;
                    httpCookie.Values["IsAuthorized"] = value.ToString();
                    CookieBase.Cookie = httpCookie;
                }
            }
    
            public static string UserName
            {
                get
                {
                    return CookieBase.Cookie == null ? string.Empty : CookieBase.Cookie.Values["UserName"];
                }
                set
                {
                    HttpCookie httpCookie = CookieBase.Cookie == null ? CookieBase.NewCookie : CookieBase.Cookie;
                    httpCookie.Values["UserName"] = value;
                    CookieBase.Cookie = httpCookie;
                }
            }
    
            public static void RemoveCooke()
            {
                CookieBase.RemoveCookie();
            }
        }
    }
    

     

    using Insus.NET.DataBases;
    using Insus.NET.Models;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Insus.NET;
    
    namespace Insus.NET.Entities
    {
        public class FileEntity
        {
            BizSP sp = new BizSP();
            public DataTable GetFileByPrimarykey(File f)
            {
                List<Parameter> param = new List<Parameter>() {
                                        new Parameter("@Afd_nbr", SqlDbType.Int,4,f.Afd_nbr)
                };
                sp.ConnectionString = DB.ConnectionString;
                sp.Parameters = param;
                sp.ProcedureName = "usp_ApiFileDemo_GetByPrimarykey";
                return sp.ExecuteDataSet().Tables[0];
            }
    
            public void Insert(File f)
            {
                List<Parameter> param = new List<Parameter>() {
                                        new Parameter("@Picture", SqlDbType.Image,-1,f.Picture),
                                        new Parameter("@PictureType",SqlDbType.NVarChar,-1,f.PictureType),
                                        new Parameter("@FileExtension",SqlDbType.NVarChar,-1,f.FileExtension)
                };
                sp.ConnectionString = DB.ConnectionString;
                sp.Parameters = param;
                sp.ProcedureName = "usp_ApiFileDemo_Insert";
                sp.Execute();
            }
    
            public void Update(File f)
            {
                List<Parameter> param = new List<Parameter>() {
                                        new Parameter("@Afd_nbr", SqlDbType.Int,4,f.Afd_nbr),
                                        new Parameter("@Picture", SqlDbType.Image,-1,f.Picture),
                                        new Parameter("@PictureType",SqlDbType.NVarChar,-1,f.PictureType),
                                        new Parameter("@FileExtension",SqlDbType.NVarChar,-1,f.FileExtension)
                };
                sp.ConnectionString = DB.ConnectionString;
                sp.Parameters = param;
                sp.ProcedureName = "usp_ApiFileDemo_Update";
                sp.Execute();
            }
    
            public void Delete(File f)
            {
                List<Parameter> param = new List<Parameter>() {
                                        new Parameter("@Afd_nbr", SqlDbType.Int,4,f.Afd_nbr)
                };
                sp.ConnectionString = DB.ConnectionString;
                sp.Parameters = param;
                sp.ProcedureName = "usp_ApiFileDemo_Delte";
                sp.Execute();
            }
        }
    }
    

     图片上传成功之后,今后我们要求把图片彰显出来。
    鉴于存款和储蓄的是二进制的数据流,显示图片时,供给管理一下,须求写三个自定义的Result,如:PictureResult,它须要继续ContentResult:
    澳门皇家赌场55533网址 30

    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index</title>
    
        <style type="text/css">
            #logincontact label {
                display: inline-block;
                width: 100px;
                text-align: right;
            }
    
            #logincontact_submit {
                padding-left: 100px;
            }
    
            #logincontact div {
                margin-top: 1em;
            }
    
            .error {
                display: none;
                margin-left: 10px;
            }
    
            .error_show {
                color: red;
                margin-left: 10px;
            }
    
            input.invalid {
                border: 2px solid red;
            }
    
            input.valid {
                border: 2px solid green;
            }
        </style>
    
        <script src="~/Scripts/jquery-2.2.1.js"></script>
    
        <script type="text/javascript">
            ////<![CDATA[
            $(document).ready(function () {
                $('#logincontact_Account').on('input', function () {
                    var input = $(this);
                    var is_Account = input.val();
                    if (is_Account) {
                        input.removeClass("invalid").addClass("valid");
                    }
                    else {
                        input.removeClass("valid").addClass("invalid");
                    }
                });
    
    
                $('#logincontact_Password').on('input', function () {
                    var input = $(this);
                    var is_Password = input.val();
                    if (is_Password) {
                        input.removeClass("invalid").addClass("valid");
                    }
                    else {
                        input.removeClass("valid").addClass("invalid");
                    }
                });
    
    
                $('#ButtonSignIn').click(function (event) {
                    var form_data = $("#logincontact").serializeArray();
                    var error_free = true;
                    for (var input in form_data) {
                        var element = $("#logincontact_"   form_data[input]['name']);
                        var valid = element.hasClass("valid");
                        var error_element = $("span", element.parent());
    
                        if (!valid) {
                            error_element.removeClass("error").addClass("error_show");
                            error_free = false;
                        }
                        else {
                            error_element.removeClass("error_show").addClass("error");
                        }
                    }
    
                    if (!error_free) {
                        event.preventDefault();
                    }
                    else {
                        var obj = {};
                        obj.Account = $('#logincontact_Account').val(),
                        obj.Password = $('#logincontact_Password').val()
    
                        $.ajax({
                            type: 'POST',
                            url: '/Home/LoginVerify',
                            dataType: 'json',
                            data: JSON.stringify(obj),
                            contentType: 'application/json; charset=utf-8',
                            success: function (data, textStatus) {
                                alert("登录成功。");
                                window.location.href = "/User/Index";
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(errorThrown);
                            },
                        });
                    }
                });
            });
            //]]>
        </script>
    </head>
    <body>
        <form id="logincontact" method="post" action="">
            <div>
                <label for="logincontact_Account">Account:</label>
                <input type="text" id="logincontact_Account" name="Account" />
                This account field is required.
            </div>
            <div>
                <label for="logincontact_Password">Password:</label>
                <input type="password" id="logincontact_Password" name="Password" />
                This password field is required.
            </div>
            <div id="logincontact_submit">
                <input id="ButtonSignIn" type="button" value="Sign In" />
            </div>
        </form>
    </body>
    </html>
    
    DECLARE @r int = 1,@rs INT = 0
    SELECT @rs = MAX([empid]) FROM #ttt
    
    WHILE @r <= @rs 
    BEGIN    
        IF (@r = 1 )
            UPDATE #ttt SET [eqty] = [qty] WHERE [id] = @r --处理第一笔记录
        ELSE
        BEGIN
            DECLARE @eqty INT 
            SELECT @eqty = [eqty] FROM #ttt WHERE [id] = @r - 1  --获取前一笔的结余数
    
            UPDATE #ttt SET [eqty] = [qty]   @eqty  WHERE [id] = @r  --计算后更新当前记录的eqty字段值。
        END    
    
        SET @r = @r  1
    END
    GO
    

    Source Code

     
    Web API完结,大家须要把它揭露至IIS中去,怎样宣布,能够参见《始建与应用Web API》......

     

    本文由68399皇家赌场发布于虚拟主机,转载请注明出处:Web API与公事操作

    关键词: 68399皇家赌场 ASP.NET ASP.NET MVC .NET技术

上一篇:Ruby操作Excel【转】

下一篇:没有了