Controllers/MessageController.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using FSBlog.Models;

namespace FSBlog.Controllers
{
    public class MessageController : Controller
    {
        /// <summary>
        /// 注入目录
        /// </summary>
        private readonly IHostingEnvironment _hostingEnvironment;

        public MessageController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }


        public IActionResult Index(string uname, string uemail, string txt)
        {
            XmlDocument doc = new XmlDocument();
            // 存放消息
            if (!string.IsNullOrEmpty(uname) && !string.IsNullOrEmpty(uemail) && !string.IsNullOrEmpty(txt))
            {
                doc.Load(_hostingEnvironment.WebRootPath + @"\data\board.xml");
                XmlElement root = doc.DocumentElement;
                int id = 0;
                if (root.ChildNodes.Count > 0)//如果没有消息就从0开始
                {
                    id = Convert.ToInt32(root.LastChild.Attributes["id"].Value);
                }
                ++id;
                XmlElement message = doc.CreateElement("message");
                message.SetAttribute("id", id.ToString());
                message.SetAttribute("ip", HttpContext.Session.GetString("uip"));
                XmlElement name = doc.CreateElement("name");
                XmlElement email = doc.CreateElement("email");
                XmlElement time = doc.CreateElement("time");
                XmlElement content = doc.CreateElement("content");
                name.InnerText = uname;
                email.InnerText = uemail;
                time.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                content.InnerText = txt;
                message.AppendChild(name);
                message.AppendChild(email);
                message.AppendChild(time);
                message.AppendChild(content);
                root.AppendChild(message);
                doc.Save(_hostingEnvironment.WebRootPath + @"\data\board.xml");
            }
            // 读取消息
            List<Message> list = new List<Message>();
            doc.Load(_hostingEnvironment.WebRootPath + @"\data\board.xml");
后面还有 20 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。