在添加控制器前,先增加一些用到的类
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);
}
}
}
|