关键字读写模块

前段时间,在项目的管理员后台做了一个简单的关键字读写的模块。这个模块采用ASP.NET MVC 2.0,主要实现的功能是管理员可以在后台将需要的关键字写入到指定路径的一个txt文件里面。然后项目其他站点通过这个读取这个文件,获取需要过滤的关键字,对站点用户输入的内容做判断。

1.首先在管理员后台新建一个Area,命名为KeyWord.KeyWord的目录结构和根目录下的Controllers,Models,Views是一样的,唯一的区别是在KeyWord目录下多了一个KeyWordAreaRegistration.cs文件。它继承自AreaRegistration类,KeyWordAreaRegistration必须实现AreaRegistration类中的AreaName属性和RegisterArea(AreaRegistrationContext context)方法.

imageimage

KeyWordAreaRegistration.cs代码如下所示:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Mvc;
   6:  
   7: namespace QQYDT.Admin.Areas.KeyWord
   8: {
   9:     public class KeyWordAreaRegistration : AreaRegistration
  10:     {
  11:         public override string AreaName
  12:         {
  13:             get
  14:             {
  15:                 return "KeyWord";
  16:             }
  17:         }
  18:  
  19:         public override void RegisterArea(AreaRegistrationContext context)
  20:         {
  21:             context.MapRoute(
  22:                 "KeyWord_default",
  23:                 "KeyWord/{controller}/{action}/{id}",
  24:                 new { controller = "KeyWord", action = "Write", id = UrlParameter.Optional },
  25:                 new string[] { "QQYDT.Admin.Controllers" }
  26:             );
  27:         }
  28:     }
  29: }

AreaRegistrationContext中MapRoute的方法原型是public Route MapRoute(string name, string url, string[] namespaces);

使用指定的路由默认值和命名空间,映射指定的 URL 路由并将其与System.Web.Mvc.AreaRegistrationContext.AreaName 属性指定的区域关联.

name:  路由的名称。

url:  路由的 URL 模式。

defaults:  一个包含默认路由值的对象。
namespaces:  应用程序的一组可枚举的命名空间。

这里需要注意的是上面调用的context.MapRoute必须指定第四个参数,默认路由值的controller命名空间,否则访问的时候会找不到指定的action的错误。

 

2.在KeyWord目录的Controllers目录下添加一个新的控制器KeyWordController.cs.

image

在里面定义关键字读写的方法.具体代码如下:

 
   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5: using System.Web.Mvc;
   6: using System.Text;
   7: using System.IO;
   8: using System.Configuration;
   9:  
  10:  
  11: namespace QQYDT.Admin.Controllers
  12: {
  13:     
  14:     public class KeyWordController : BaseController
  15:     {   
  16:        
  17:         /// <summary>
  18:        /// 将文件内容能够存入ViewData["content"]并返回write页面
  19:         /// </summary>
  20:         /// <returns></returns>
  21:        [Permission]
  22:        public ActionResult Write()
  23:        {
  24:            ViewData["content"] = ReadFile();
  25:            return View();
  26:        }
  27:  
  28:         /// <summary>
  29:         /// 将编辑的关键字信息写入文件
  30:         /// </summary>
  31:         /// <param name="content">待写入文件的内容</param>
  32:         /// <returns>返回关键字信息页面或者错误提示页面</returns>
  33:        [Permission]
  34:        [HttpPost]
  35:        public ActionResult Write(string content)
  36:         {
  37:             string s = content;
  38:             if (!String.IsNullOrEmpty(s))
  39:             {
  40:                 ViewData["content"] = "";
  41:                 WriteFile(s);
  42:                 ViewData["content"] = ReadFile();
  43:                 return View();
  44:             }
  45:             else
  46:             {
  47:                 return Error("返回", "对不起,录入的关键字为空,请录入关键字后再点保存");
  48:             }
  49:         }
  50:  
  51:  
  52:        / <summary>
  53:        / 读文件
  54:        / </summary>
  55:        / <returns>文件的内容</returns>
  56:        public static string ReadFile()
  57:        {
  58:            ///从webconfig文件里面读取存储关键字文件的路径
  59:            String rpath = ConfigurationManager.AppSettings["KeyWord"].ToString();
  60:            FileStream fs = new FileStream(rpath, FileMode.OpenOrCreate, FileAccess.Read);
  61:            StreamReader m_streamReader = new StreamReader(fs,System.Text.Encoding.GetEncoding("utf-8"));
  62:            //使用StreamReader类来读取文件
  63:            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
  64:            // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
  65:            StringBuilder sb = new StringBuilder();
  66:            string strLine = m_streamReader.ReadLine();
  67:            while (!String.IsNullOrEmpty(strLine))
  68:            {
  69:                    sb.Append(strLine);
  70:                    sb.Append("\n");
  71:                    strLine = m_streamReader.ReadLine();
  72:            }
  73:            //关闭此StreamReader对象
  74:            m_streamReader.Close();
  75:            return sb.ToString();
  76:  
  77:        }
  78:  
  79:  
  80:        /// <summary>
  81:        /// 写关键字到txt文件
  82:        /// </summary>
  83:        /// <param name="content">要写入的内容</param>
  84:        public static void WriteFile(string content)
  85:        {
  86:            String wpath = ConfigurationManager.AppSettings["KeyWord"].ToString();
  87:            Encoding u8 = Encoding.UTF8;
  88:            FileStream fs = new FileStream(wpath, FileMode.OpenOrCreate, FileAccess.Write);
  89:            StreamWriter m_streamWriter = new StreamWriter(fs, u8);
  90:            m_streamWriter.Flush();
  91:            // 使用StreamWriter来往文件中写入内容
  92:            m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
  93:            // 把richTextBox1中的内容写入文件
  94:            m_streamWriter.WriteLine(content);
  95:            // 将行结束符写入文件
  96:            //m_streamWriter.WriteLine();
  97:            //关闭此文件
  98:            m_streamWriter.Flush();
  99:            m_streamWriter.Close();
 100:        }
 101:     }
 102: }

3.在Views目录下面新建一个与KeyWordController对应的文件夹KeyWord.然后在里面添加一个新的视图,命名为Write.aspx,与Controller里面的public ActionResult Write()对应。这里一定要新建一个KeyWord文件夹,如果在Views目录下直接添加视图的话,请求action的时候控制器会找不到指定的action而报错。

Write.aspx的代码如下所示:

   1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/ViewPage.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
   2:  
   3: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
   4:     读写关键字
   5: </asp:Content>
   6:  
   7: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">  
   8:      <form action="" method="post">
   9:      <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  10:      <tr>
  11:      <td colspan="2">
  12:      <textarea id="Content" name="Content" rows="10" cols="100" style="width: 98%; height: 300px;"><%=ViewData["content"]%></textarea>
  13:      </td>
  14:      </tr>
  15:      <tr>
  16:      <td><input type="submit" id="bsubmit" name="bsubmit" value="保存"/></td>
  17:      </tr>
  18:      </table>
  19:      </form>
  20: </asp:Content>

4.在项目根目录下的Web.config而不是区域的Web.config配置好文件的路径.

<add key="KeyWord" value="E:\\workspace\QQYDT.App\Views\keyword.txt" />

转载于:https://www.cnblogs.com/jerry01/archive/2012/08/31/2665152.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值