IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> (7) 添加控制器 -> 正文阅读

[开发工具](7) 添加控制器

在添加控制器前,先增加一些用到的类

code\utils\MyResult.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace core_admin.utils
{
    public class MyResult
    {
        public string result { get; set; }

        public string err_msg { get; set; }

        public int total { get; set; }

        public int page_size {get;set;}

        public int page_id {get;set;}

        public object data { get; set; }

        public Hashtable viewbag { get; set; }
    }
}

code\utils\StringHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace core_admin.utils
{
    public class StringHelper
    {


        public static string byteToBase64(byte[] b)
        {
            return System.Convert.ToBase64String(b);
        }

        public static byte[] base64ToByte(string b)
        {
            return System.Convert.FromBase64String(b);
        }

        public static object Convert(string str, string type)
        {
            switch (type)
            {
                case "string":
                    return str;
                case "int":
                    return Int32.Parse(str);
                case "long":
                    return Int64.Parse(str);
                case "DateTime":
                    try
                    {
                        return DateTime.Parse(str);
                    } catch (Exception ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        return DateTime.Now;
                    }
            }
            return "";
        }
        public static string showIfNotEmpty(string words,string all)
        {
            if (string.IsNullOrEmpty(words))
            {
                return "";
            }
            else
                return all;
        }

        public static string formatNumber(long num)
        {
            string str = num.ToString();
            string ret = "";
            int i = str.Length;
            while(i%3!=0){
                i=i+1;
                str =  " "+str;
            }
            for(int j=0;j<i/3;j++){
                if(j>0){
                    ret += ",";
                }
                ret += str.Substring(j,3);
            }
            
            return ret;
        }
    }
}

code\utils\PagerHelper.cs


using System.Text;

namespace core_admin.utils
{

    public class PagerHelper{

        public static string getPagerHtml(int page_id,int page_size,int total,string url){
            
            StringBuilder sb = new StringBuilder();
            int page_count = total % page_size==0? total / page_size : total / page_size + 1;
            string a_url = url ;
            string b_url = url;
            string first_url = url;
            if(url.IndexOf("?")!=-1){
                a_url = url + "&page_id="+(page_id-1).ToString();
                b_url = url + "&page_id="+(page_id+1).ToString();
                first_url = url + "&page_id=1";
            } else {
                a_url = url + "?page_id="+(page_id-1).ToString();
                b_url = url + "?page_id="+(page_id+1).ToString();
                first_url = url + "?page_id=1";
            }
            if(page_id==1){                
                sb.Append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\""+
                    a_url+"\">Previous</a></li>");
                sb.Append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\""+
                    first_url+"\">1</a></li>");
            } else {
                sb.Append("<li class=\"page-item\"><a class=\"page-link\" href=\""+
                    a_url+"\">Previous</a></li>");
                sb.Append("<li class=\"page-item\"><a class=\"page-link\" href=\""+
                    first_url+"\">1</a></li>");
            }
            bool in_ingore = false;

            for(int i=2;i<=page_count;i++){
                if(url.IndexOf("?")!=-1){
                    a_url = url + "&page_id="+i.ToString();
                } else {
                    a_url = url + "?page_id="+i.ToString();
                }
                int distance = i-page_id;
                if(distance<0){
                    distance = -distance;
                }
                if(distance<=7||page_count==i){
                    if(i==page_id){
                        sb.Append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\""+
                            a_url+"\">"+i.ToString()+"</a></li>");
                    } else {
                        sb.Append("<li class=\"page-item\"><a class=\"page-link\" href=\""+
                            a_url+"\">"+i.ToString()+"</a></li>");
                    }
                    in_ingore = false;
                } else {
                    if(in_ingore==false){
                        sb.Append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\""+
                            a_url+"\">"+"..."+"</a></li>");
                        in_ingore = true;
                    }
                }
                
            }
            if(page_id==page_count){                
                sb.Append("<li class=\"page-item disabled\"><a class=\"page-link\" href=\""+
                    b_url+"\">Next</a></li>");
            } else {
                sb.Append("<li class=\"page-item\"><a class=\"page-link\" href=\""+
                    b_url+"\">Next</a></li>");
            }
            return sb.ToString();
        }


    }
}

code\utils\Encryp.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace core_admin.utils
{
    public class Encryp
    {
        public static string Get_MD5(string strSource)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();

            //获取密文字节数组
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));

            //转换成字符串,并取9到25位
            string strResult = BitConverter.ToString(bytResult, 4, 8);
            //转换成字符串,32位
            //string strResult = BitConverter.ToString(bytResult);

            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符,需要去除掉
            strResult = strResult.Replace("-", "");
            return strResult;
        }

        public static string GetFileMD5(string filepath)
        {
            FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
            int bufferSize = 1048576;
            byte[] buff = new byte[bufferSize];
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            md5.Initialize();
            long offset = 0;
            while (offset < fs.Length)
            {
                long readSize = bufferSize;
                if (offset + readSize > fs.Length)
                    readSize = fs.Length - offset;
                fs.Read(buff, 0, Convert.ToInt32(readSize));
                if (offset + readSize < fs.Length)
                    md5.TransformBlock(buff, 0, Convert.ToInt32(readSize), buff, 0);
                else
                    md5.TransformFinalBlock(buff, 0, Convert.ToInt32(readSize));
                offset += bufferSize;
            }
            if (offset >= fs.Length)
            {
                fs.Close();
                byte[] result = md5.Hash;
                md5.Clear();
                StringBuilder sb = new StringBuilder(32);
                for (int i = 0; i < result.Length; i++)
                    sb.Append(result[i].ToString("X2"));
                return sb.ToString();
            }
            else
            {
                fs.Close();
                return null;
            }
        }
    }
}

补充两个包引用:

dotnet add package System.Drawing.Common

dotnet add package MetadataExtractor

在MyWebApp.csproj里增加了

<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
<PackageReference Include="MetadataExtractor" Version="2.7.2" />

Controllers\AdminBlogController.cs

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Collections;
using core_admin.db;
using core_admin.utils;
using core_admin.i;
using core_admin.m;
using core_admin.d;
using Autofac;

namespace core_admin.Controllers
{
    public class AdminBlogController : Controller
    {
        [HttpGet]
        public IActionResult Index(int page_id = 1,string filter="")
        {
     
            int total = 0;
            MyResult r = new MyResult();
            Hashtable viewbag = new Hashtable();
			viewbag.Add("filter", filter);
 

            using (var scope = AutofacConfig.Container.BeginLifetimeScope())
            {
                IBlog c = scope.Resolve<IBlog>();
                Filter f = new Filter();
                                                
                Hashtable containSet = new Hashtable();
				containSet.Add("filter", filter);
				f.containSet = containSet;
    
                                      
                List<blog> list = c.GetList(f, page_id, WebHelper.page_size, out total);
                r.total = total;
                r.page_id = page_id;
                r.page_size = WebHelper.page_size;
                r.result = "success";
                r.data = list;
                r.viewbag = viewbag;
            }

            SessionHelper helper = new SessionHelper(HttpContext);
            helper.SetSession("filter",filter);

            helper.SetSession("page_id",page_id.ToString());

            return View(r);
        }

        [HttpGet]
        public IActionResult Add()
        {
            return View();
        }

      

        [HttpPost]
        public string EditSave()
        {
            try
            {
                

                long i_blog_id = (long)StringHelper.Convert(Request.Form["blog_id"],"long");

                using (var scope = AutofacConfig.Container.BeginLifetimeScope())
                {
                    IBlog c = scope.Resolve<IBlog>();
                    blog bean = c.Get(i_blog_id);
                    if (bean == null)
                    {                        
                        return "未找到编辑项";
                    }
                                        
					bean.blog_title = (string)StringHelper.Convert(Request.Form["blog_title"], "string");
					bean.blog_image = (string)StringHelper.Convert(Request.Form["blog_image"], "string");
					bean.blog_desc = (string)StringHelper.Convert(Request.Form["blog_desc"], "string");
					bean.blog_content = (string)StringHelper.Convert(Request.Form["blog_content"], "string");
					bean.blog_content_text = (string)StringHelper.Convert(Request.Form["blog_content_text"], "string");
					bean.add_date = (DateTime)StringHelper.Convert(Request.Form["add_date"], "DateTime");
					bean.blog_sort = (int)StringHelper.Convert(Request.Form["blog_sort"], "int");
					bean.blog_tags = (string)StringHelper.Convert(Request.Form["blog_tags"], "string");

    
                    c.Update(bean);
                    
                }
            }
            catch (Exception ex)
            {
                
                return ex.Message;
            }
            return "success";
        }

        public IActionResult Ret(){

            SessionHelper helper = new SessionHelper(HttpContext);
            string filter = helper.GetSession("filter");
            string page_id = helper.GetSession("page_id");
            if(page_id==""){
                page_id = "1";
            }
            return RedirectToAction("Index",new {filter=filter,page_id=page_id});
        }

        [HttpPost]
        public string AddSave()
        {
            try
            {   
                blog bean = new blog();

                using (var scope = AutofacConfig.Container.BeginLifetimeScope())
                {
                    IBlog c = scope.Resolve<IBlog>();

					bean.blog_title = (string)StringHelper.Convert(Request.Form["blog_title"], "string");
					bean.blog_image = (string)StringHelper.Convert(Request.Form["blog_image"], "string");
					bean.blog_desc = (string)StringHelper.Convert(Request.Form["blog_desc"], "string");
					bean.blog_content = (string)StringHelper.Convert(Request.Form["blog_content"], "string");
					bean.blog_content_text = (string)StringHelper.Convert(Request.Form["blog_content_text"], "string");
					bean.add_date = (DateTime)StringHelper.Convert(Request.Form["add_date"], "DateTime");
					bean.blog_sort = (int)StringHelper.Convert(Request.Form["blog_sort"], "int");
					bean.blog_tags = (string)StringHelper.Convert(Request.Form["blog_tags"], "string");
                                 

                    c.Insert(bean);                                        
                }

            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return "success";
        }

        [HttpGet]
        public IActionResult Edit(string blog_id)
        {
            MyResult r = new MyResult();
            Hashtable viewbag = new Hashtable();
            Filter filter = new Filter();

            r.result = "error";
            r.viewbag = viewbag;

            using (var scope = AutofacConfig.Container.BeginLifetimeScope())
            {
                IBlog c = scope.Resolve<IBlog>();

                blog bean = c.Get((long)StringHelper.Convert(blog_id, "long"));
                if (bean != null)
                {
                    r.result = "success";
                    r.data = bean;
                }
                else
                {
                    r.err_msg = "未找到数据";
                }
            }
            return View(r);
        }

         [HttpGet]
        public IActionResult Detail(string blog_id)
        {
            MyResult r = new MyResult();
            Hashtable viewbag = new Hashtable();
            Filter filter = new Filter();
            r.result = "error";
            r.viewbag = viewbag;

            using (var scope = AutofacConfig.Container.BeginLifetimeScope())
            {
                IBlog c = scope.Resolve<IBlog>();

                blog bean = c.Get((long)StringHelper.Convert(blog_id, "long"));
                if (bean != null)
                {
                    r.result = "success";
                    r.data = bean;
                }
                else
                {
                    r.err_msg = "未找到数据";
                }
            }
            return View(r);
        }

        

        [HttpGet]
        public string Delete(string blog_id)
        {
            MyResult result = new MyResult();
            result.result = "error";
            using (var scope = AutofacConfig.Container.BeginLifetimeScope())
            {
                IBlog c = scope.Resolve<IBlog>();
                blog bean = c.Get((long)StringHelper.Convert(blog_id, "long"));
                if (bean != null)
                {
                    c.Delete((long)StringHelper.Convert(blog_id, "long"));                    
                    result.result = "success";
                }
                else
                {
                    result.err_msg = "未找到要删除的数据";
                }
            }
        
            return JsonConvert.SerializeObject(result);

        }        

    }
}


  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2022-03-30 18:46:03  更:2022-03-30 18:50:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 5:22:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码