【C#MVC工具】C#MVC中使用Dictionary
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【C#MVC工具】C#MVC中使用Dictionary,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3663字,纯文字阅读大概需要6分钟。
内容图文
![【C#MVC工具】C#MVC中使用Dictionary](/upload/InfoBanner/zyjiaocheng/613/ed4a7fcfae204605930f5e62a7e6bb72.jpg)
【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();
内容总结
以上是互联网集市为您收集整理的【C#MVC工具】C#MVC中使用Dictionary全部内容,希望文章能够帮你解决【C#MVC工具】C#MVC中使用Dictionary所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。