【C#MVC工具】C#MVC中使用Dictionary
【C#MVC工具】C#MVC中使用Dictionary
今天封装了一个工具类,具体作用是将Request传递过来的参数放到Dictionary中,可以对参数进行优化,也可以减少大量的重复代码。 这里也可以通过这个解决。有没有可能对其进行封装,并加以优化呢。这里推出一个工具类,PageData。
PagaData:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace SAM.Common
{
public class PageData:Dictionary<object,object>,IDictionary<object,object>
{
IDictionary<object, object> dictionary = null;
HttpRequestBase request = null;
public PageData(HttpRequestBase request) {
this.request = request;
Dictionary<object, object> returnDictory = new Dictionary<object, object>();
string[] keys = request.Form.AllKeys; //Set
if(keys.Count() == 0)
{
keys = request.QueryString.AllKeys; //Get
}
foreach(string name in keys){
string value = request.Params[name].ToString().Trim();
returnDictory.Add(name,value);
}
dictionary = returnDictory;
}
public PageData()
{
dictionary = new Dictionary<object, object>();
}
public Object Get(object key)
{
return dictionary[key];
}
//public DateTime GetDateTime(object key) {<!-- -->
// if (key != null)
// {<!-- -->
// return Utility.ConvertToDatetime(dictionary[key]);
// }
// else
// {<!-- -->
// return DateTime.Now;
// }
//}
//public Int32 GetInt32(object key)
//{<!-- -->
// return Utility.ConvertToInt32(dictionary[key]);
//}
public Object this[object key] {
get { return dictionary[key]; }
set { }
}
public void Add(object key, object value)
{
dictionary.Add(key,value);
}
public bool ContainsKey(object key)
{
return dictionary.ContainsKey(key);
}
public ICollection<object> Keys
{
get { return dictionary.Keys; }
}
public bool Remove(object key)
{
return dictionary.Remove(key);
}
public ICollection<object> Values
{
get { return dictionary.Values; }
}
public void Add(KeyValuePair<object, object> item)
{
dictionary.Add(item);
}
public void Clear()
{
dictionary.Clear();
}
public bool Contains(KeyValuePair<object, object> item)
{
return dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex)
{
dictionary.CopyTo(array,arrayIndex);
}
public int Count
{
get { return dictionary.Count; }
}
public bool IsReadOnly
{
get { return dictionary.IsReadOnly; }
}
public bool Remove(KeyValuePair<object, object> item)
{
return dictionary.Remove(item);
}
public IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return dictionary.GetEnumerator();
}
}
}
这里有两个构造函数,第一个就是你在action中,通过Request,获取到页面传递的数据。第二个就是你可以把PagaData当做一个Dictionary类来使用。
在action如何调用呢?
你可以在你的BaseController中,添加一个Get方法。然后用Conntroller继承BaseController,这个BaseController还可以做其他操作,例如Session的检查,异常信息的捕获,日志信息等。
BaseController:
using SAM.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SAM.Web.Controllers
{
public class BaseController : Controller
{
//
// GET: /Base/
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
string errorMessage = LogHelper.RecordError(filterContext.Exception);
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Redirect("/Login/ErrorPage?msg=" + errorMessage);//跳转至错误提示页面
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (Session[SessionKey.USER_MASTER] == null || Session[SessionKey.COMPANY_INFORMATION] == null)
{
Response.Redirect("/Login/ReLogin");
}
}
public PageData GetPageData()
{
return new PageData(this.Request);
}
}
}
调用:
Controller:
PageData pd = this.GetPageData();