C# 在MVC(httpmodule)中将英文数字更改为波斯语,反之亦然?
我想把所有英文数字都改成波斯语,以便向用户显示。并再次将其更改为英文数字,以提供所有请求(回发)C# 在MVC(httpmodule)中将英文数字更改为波斯语,反之亦然?,c#,asp.net,asp.net-mvc-3,httpmodule,C#,Asp.net,Asp.net Mvc 3,Httpmodule,我想把所有英文数字都改成波斯语,以便向用户显示。并再次将其更改为英文数字,以提供所有请求(回发) 例如: 我们在查看IRQ170时有类似的内容,我想向用户展示IRQ۱۷۰,并向用户提供IRQ170 我知道,我必须使用Httpmodule,但我不知道如何使用? 你能指引我吗 编辑: 让我再描述一下: 我编写了以下http模块: using System; using System.Collections.Specialized; using System.Diagnostics; using Sy
例如:
我们在查看
IRQ170
时有类似的内容,我想向用户展示IRQ۱۷۰
,并向用户提供IRQ170
我知道,我必须使用Httpmodule
,但我不知道如何使用?你能指引我吗 编辑: 让我再描述一下:
我编写了以下http模块:
using System;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using Smartiz.Common;
namespace Smartiz.UI.Classes
{
public class PersianNumberModule : IHttpModule
{
private StreamWatcher _watcher;
#region Implementation of IHttpModule
/// <summary>
/// Initializes a module and prepares it to handle requests.
/// </summary>
/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application </param>
public void Init(HttpApplication context)
{
context.BeginRequest += ContextBeginRequest;
context.EndRequest += ContextEndRequest;
}
/// <summary>
/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
/// </summary>
public void Dispose()
{
}
#endregion
private void ContextBeginRequest(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
if (context == null) return;
_watcher = new StreamWatcher(context.Response.Filter);
context.Response.Filter = _watcher;
}
private void ContextEndRequest(object sender, EventArgs e)
{
HttpApplication context = sender as HttpApplication;
if (context == null) return;
_watcher = new StreamWatcher(context.Response.Filter);
context.Response.Filter = _watcher;
}
}
public class StreamWatcher : Stream
{
private readonly Stream _stream;
private readonly MemoryStream _memoryStream = new MemoryStream();
public StreamWatcher(Stream stream)
{
_stream = stream;
}
public override void Flush()
{
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
int bytesRead = _stream.Read(buffer, offset, count);
string orgContent = Encoding.UTF8.GetString(buffer, offset, bytesRead);
string newContent = orgContent.ToEnglishNumber();
int newByteCountLength = Encoding.UTF8.GetByteCount(newContent);
Encoding.UTF8.GetBytes(newContent, 0, Encoding.UTF8.GetByteCount(newContent), buffer, 0);
return newByteCountLength;
}
public override void Write(byte[] buffer, int offset, int count)
{
string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
MatchCollection htmlAttributes = Regex.Matches(strBuffer, @"(\S+)=[""']?((?:.(?![""']?\s+(?:\S+)=|[>""']))+.)[""']?", RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach (Match match in htmlAttributes)
{
strBuffer = strBuffer.Replace(match.Value, match.Value.ToEnglishNumber());
}
MatchCollection scripts = Regex.Matches(strBuffer, "<script[^>]*>(.*?)</script>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach (Match match in scripts)
{
MatchCollection values = Regex.Matches(match.Value, @"([""'])(?:(?=(\\?))\2.)*?\1", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach (Match stringValue in values)
{
strBuffer = strBuffer.Replace(stringValue.Value, stringValue.Value.ToEnglishNumber());
}
}
MatchCollection styles = Regex.Matches(strBuffer, "<style[^>]*>(.*?)</style>", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach (Match match in styles)
{
strBuffer = strBuffer.Replace(match.Value, match.Value.ToEnglishNumber());
}
byte[] data = Encoding.UTF8.GetBytes(strBuffer);
_memoryStream.Write(data, offset, count);
_stream.Write(data, offset, count);
}
public override string ToString()
{
return Encoding.UTF8.GetString(_memoryStream.ToArray());
}
#region Rest of the overrides
public override bool CanRead
{
get { throw new NotImplementedException(); }
}
public override bool CanSeek
{
get { throw new NotImplementedException(); }
}
public override bool CanWrite
{
get { throw new NotImplementedException(); }
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override long Length
{
get { throw new NotImplementedException(); }
}
public override long Position
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
#endregion
}
}
使用系统;
使用System.Collections.Specialized;
使用系统诊断;
使用System.IO;
使用系统文本;
使用System.Text.RegularExpressions;
使用System.Web;
使用System.Web.UI;
使用Smartiz.Common;
命名空间Smartiz.UI.Classes
{
公共类PersianumberModule:IHttpModule
{
私人StreamWatcher_watcher;
#IHttpModule的区域实现
///
///初始化模块并使其准备好处理请求。
///
///提供对ASP.NET应用程序中所有应用程序对象通用的方法、属性和事件的访问的
公共void Init(HttpApplication上下文)
{
context.BeginRequest+=ContextBeginRequest;
context.EndRequest+=ContextEndRequest;
}
///
///处置由实现的模块使用的资源(内存除外)。
///
公共空间处置()
{
}
#端区
私有void ContextBeginRequest(对象发送方,事件参数e)
{
HttpApplication context=发送方作为HttpApplication;
if(context==null)返回;
_watcher=新的StreamWatcher(context.Response.Filter);
context.Response.Filter=\u watcher;
}
私有void ContextEndRequest(对象发送方,事件参数e)
{
HttpApplication context=发送方作为HttpApplication;
if(context==null)返回;
_watcher=新的StreamWatcher(context.Response.Filter);
context.Response.Filter=\u watcher;
}
}
公共类StreamWatcher:流
{
私有只读流_流;
私有只读内存流_MemoryStream=new MemoryStream();
公共StreamWatcher(Stream)
{
_溪流=溪流;
}
公共覆盖无效刷新()
{
_stream.Flush();
}
公共重写整型读取(字节[]缓冲区、整型偏移量、整型计数)
{
int bytesRead=_stream.Read(缓冲区、偏移量、计数);
string orgContent=Encoding.UTF8.GetString(缓冲区、偏移量、字节读取);
字符串newContent=orgContent.ToEnglishNumber();
int newByteCountLength=Encoding.UTF8.GetByteCount(newContent);
Encoding.UTF8.GetBytes(newContent,0,Encoding.UTF8.GetByteCount(newContent),buffer,0);
返回新字节计数长度;
}
公共重写无效写入(字节[]缓冲区、整数偏移量、整数计数)
{
string strBuffer=Encoding.UTF8.GetString(缓冲区、偏移量、计数);
MatchCollection HtmLatAttributes=Regex.Matches(strBuffer,@“(\S+)=[”“”]?((?:(?![”“”)?\S+(?:\S+)=[>”)+)[”“”?”,RegexOptions.IgnoreCase | RegexOptions.Multiline);
foreach(HtmLatAttributes中的匹配)
{
strBuffer=strBuffer.Replace(match.Value,match.Value.ToEnglishNumber());
}
MatchCollection scripts=Regex.Matches(strBuffer,“]*>(.*)”,RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach(脚本中的匹配)
{
MatchCollection values=Regex.Matches(match.Value,@“([“”])(?:(?=(\\?)\2.)*?\1”,RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach(在值中匹配stringValue)
{
strBuffer=strBuffer.Replace(stringValue.Value,stringValue.Value.ToEnglishNumber());
}
}
MatchCollection styles=Regex.Matches(strBuffer,“]*>(.*)”,RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
foreach(样式匹配)
{
strBuffer=strBuffer.Replace(match.Value,match.Value.ToEnglishNumber());
}
byte[]data=Encoding.UTF8.GetBytes(strBuffer);
_memoryStream.Write(数据、偏移量、计数);
_流写入(数据、偏移量、计数);
}
公共重写字符串ToString()
{
返回Encoding.UTF8.GetString(_memoryStream.ToArray());
}
#区域覆盖的其余部分
公共覆盖布尔可读取
{
获取{抛出新的NotImplementedException();}
}
公共覆盖布尔搜索
{
获取{抛出新的NotImplementedException();}
}
公共覆盖布尔可写
{
获取{抛出新的NotImplementedException();}
}
公共覆盖长寻道(长偏移,参见原始坐标系)
{
抛出新的NotImplementedException();
}
公共覆盖无效设置长度(长值)
{
抛出新的NotImplementedException();
}
公共覆盖长长度
{
获取{抛出新的NotImplementedException();}
}
公众优先多头仓位
{
得到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
#端区
}
}
它起作用了