计算结余数

2019-07-15 02:31栏目:程序人生

 

近年来,平昔有练习ASP.NET MVC与Web API交互,接下去,Insus.NET再做一些连锁的演练,Web API与公事操作,如POST文件至Web API,更新或是删除等。

Source Code

利用Cookie来积攒登入以及表达音信,写一个Cookie类别:
ca888会员登录 1

兑现那些职能,关键是收获前一笔记录eqty字段的值。

Source Code

CREATE TABLE ApiFileDemo
(
    [Afd_nbr] INT IDENTITY(1,1) PRIMARY KEY NOT NULL,
    [Picture] [image] NULL,
    [PictureType] [nvarchar](30) NULL,
    [FileExtension] [nvarchar](10) NULL
)
GO

CREATE PROCEDURE [dbo].[usp_ApiFileDemo_Insert]
(    
    @Picture IMAGE,
    @PictureType NVARCHAR(30),
    @FileExtension NVARCHAR(10)
)
AS
INSERT INTO [dbo].[ApiFileDemo] ([Picture],[PictureType],[FileExtension]) VALUES (@Picture,@PictureType,@FileExtension)
GO

CREATE PROCEDURE [dbo].[usp_ApiFileDemo_Update]
(
    @Afd_nbr INT,
    @Picture IMAGE,
    @PictureType NVARCHAR(30),
    @FileExtension NVARCHAR(10)
)
AS
UPDATE [dbo].[ApiFileDemo]  SET [Picture] = @Picture,[PictureType] = @PictureType,[FileExtension] = @FileExtension WHERE [Afd_nbr] = @Afd_nbr
GO

CREATE PROCEDURE [dbo].[usp_ApiFileDemo_Delte]
(
    @Afd_nbr INT
)
AS
DELETE FROM [dbo].[ApiFileDemo] WHERE [Afd_nbr] = @Afd_nbr
GO
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Insus.NET.Utilities;
using System.Web.Routing;

namespace Insus.NET.Attributes
{
    public class SecurityAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return SecurityBase.IsAuthorized;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            string actionName = filterContext.ActionDescriptor.ActionName;           
            base.OnAuthorization(filterContext);
        }

        protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
        {
            var routeValue = new RouteValueDictionary {
                { "Controller", "Home"},
                { "Action", "Index"}
            };
            filterContext.Result = new RedirectToRouteResult(routeValue);
        }
    }
}

有七个网络老铁问及,在SQL中,计算每一笔的多余数。他提供的截图注脚:
ca888会员登录 2

ca888会员登录 3

Source Code

Source Code

Source Code

写好model之后,还索要为API写叁个实体,这么些目的只是让程序与数据库举行互动。获取与积存等操作:
ca888会员登录 4

Ok,接下去,大家付出客户端的次序,尝试上Web API上传一些文书。

在ASP.NET MVC中,实现登陆验证的躬行实践,最少供给四个调整器,一个是给佚名用户访谈的,它包涵普通的页面和一部分为主的操作。另二个调整器是经过证实通过之后技艺访谈的页面。
ca888会员登录 5

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

 
接下去,我们能够设计Web API接口,待实现了,发表至英特网,别的客户端就能够操作了。

ca888会员登录 6ca888会员登录 7

 

还索要把原有数据搬到此新建的有时表中:
ca888会员登录 8

接下去,大家在调节器成立视图的Action:
ca888会员登录 9

 

Source Code

Source Code

ca888会员登录 10ca888会员登录 11

 

 

 

Source Code

ca888会员登录 12ca888会员登录 13

ca888会员登录 14ca888会员登录 15

 

 

ca888会员登录 16ca888会员登录 17

 
ca888会员登录,其一过滤器SecurityAuthorizeAttribute.cs,稍后我们会在调整器中动用到它。

ca888会员登录 18

 public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
        {
            foreach (var file in files)
            {
                if (file.ContentLength > 0)
                {
                    Insus.NET.Models.File f = new Insus.NET.Models.File();
                    f.PictureType = file.ContentType;
                    string fn = Path.GetFileName(file.FileName);
                    f.FileExtension = fn.Substring(fn.LastIndexOf('.'));
                    using (Stream inputStream = file.InputStream)
                    {
                        MemoryStream memoryStream = inputStream as MemoryStream;
                        if (memoryStream == null)
                        {
                            memoryStream = new MemoryStream();
                            inputStream.CopyTo(memoryStream);
                        }
                        f.Picture = memoryStream.ToArray();
                    }
                    HttpClient client = new HttpClient();
                    string ff = f.ToJson();
                    HttpContent httpcontent = new StringContent(ff, System.Text.Encoding.UTF8, "application/json");
                    client.PostAsync("http://localhost:9001/api/file", httpcontent)
                        .ContinueWith((postTask) =>
                        {
                            postTask.Result.EnsureSuccessStatusCode();
                        });
                }
            }
            return RedirectToAction("Upload");
        }

Source Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Insus.NET.Utilities
{
    public abstract class CookieBase
    {
        private static HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }

        private static HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        public static HttpCookie Cookie
        {
            get
            {
                return Request.Cookies["CookieBase"] as HttpCookie;
            }
            set
            {
                if (Request.Cookies["CookieBase"] != null)
                {
                    Request.Cookies.Remove("CookieBase");
                }
                Response.Cookies.Add(value);
            }
        }

        public static HttpCookie NewCookie
        {
            get
            {
                return new HttpCookie("CookieBase");
            }
        }

        public static void RemoveCookie()
        {
            if (Cookie == null)
                Response.Cookies.Remove("CookieBase");
            else
                Response.Cookies["CookieBase"].Expires = DateTime.Now.AddDays(-1);
        }       
    }
}

下边Insus.NET尝试写一下。使用最简便易行的方式,就是循环每单笔记录。然后可以总结qty加上前一笔的eqty。

ca888会员登录 19ca888会员登录 20

Source Code

@{
    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>

管理结果:
ca888会员登录 21

 

Source Code

ca888会员登录 22ca888会员登录 23

 

上边包车型大巴主宰器FileController,正是为客户端访谈的接口,那么些项目,它是三番五次了ApiController。
ca888会员登录 24

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);
        }
    }
}

无论怎么着,先在数据库创造一张表,用来储存上传的文本。本实例中是把公文存款和储蓄过数据库的。

 

版权声明:本文由ca888发布于程序人生,转载请注明出处:计算结余数