C# MVC 用户登录状态判断
来源:https://www.cnblogs.com/cherryzhou/p/4978342.html
在Filters文件夹下添加一个类AuthenticationAttribute ,代码如下:
// 登录认证特性 public class AuthenticationAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.HttpContext.Session["username"] == null) filterContext.Result = new RedirectToRouteResult("Login", new RouteValueDictionary { { "from", Request.Url.ToString() } }); base.OnActionExecuting(filterContext); } }
在Controller中的使用方法如下:
public class HomeController : Controller { [Authentication] public ActionResult Index() { return View(); } }
如果你想针对整个MVC项目的所有Action都使用此过滤器,步骤如下(a,b选择一种就好):
a. 确保Global.asax.cs的Application_Start方法中包含如下红色行:
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } }
b. 在FilterConfig.cs文件中注册相应的特性过滤器:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new AuthenticationAttribute()); } }
树立目标,保持活力,gogogo!
【C#】list 去重(转载)
Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组、集合中的重复元素,还可以自定义去重的规则。
有两个重载方法:
// // 摘要: // 通过使用默认的相等比较器对值进行比较返回序列中的非重复元素。 // // 参数: // source: // 要从中移除重复元素的序列。 // // 类型参数: // TSource: // source 中的元素的类型。 // // 返回结果: // 一个 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重复元素。 // // 异常: // System.ArgumentNullException: // source 为 null。 public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source); // // 摘要: // 通过使用指定的 System.Collections.Generic.IEqualityComparer<T> 对值进行比较返回序列中的非重复元素。 // // 参数: // source: // 要从中移除重复元素的序列。 // // comparer: // 用于比较值的 System.Collections.Generic.IEqualityComparer<T>。 // // 类型参数: // TSource: // source 中的元素的类型。 // // 返回结果: // 一个 System.Collections.Generic.IEnumerable<T>,包含源序列中的非重复元素。 // // 异常: // System.ArgumentNullException: // source 为 null。 public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer);
第一个方法不带参数,第二个方法需要传一个System.Collections.Generic.IEqualityComparer<T>的实现对象
1.值类型元素集合去重
List<int> list = new List<int> { 1, 1, 2, 2, 3, 4, 5, 5 }; list.Distinct().ToList().ForEach(s => Console.WriteLine(s));
执行结果是:1 2 3 4 5
2.引用类型元素集合去重
首先自定义一个Student类
使用不到参数的Distinct方法去重
List<Student> list = new List<Student>() { new Student("James",1,"Basketball"), new Student("James",1,"Basketball"), new Student("Kobe",2,"Basketball"), new Student("Curry",3,"Football"), new Student("Curry",3,"Yoga") }; list.Distinct().ToList().ForEach(s => Console.WriteLine(s.ToString()));
执行结果:
可见,并没有去除重复的记录。
不带comparer参数的Distinct方法是使用的IEqualityComparer接口的默认比较器进行比较的,对于引用类型,默认比较器比较的是其引用地址,程序中集合里的每一个元素都是个新的实例,引用地址都是不同的,所以不会被作为重复记录删除掉。
因此,我们考虑使用第二个重载方法。
新建一个类,实现IEqualityComparer接口。注意GetHashCode方法的实现,只有HashCode相同才会去比较
public class Compare:IEqualityComparer<Student> { public bool Equals(Student x,Student y) { return x.Id == y.Id;//可以自定义去重规则,此处将Id相同的就作为重复记录,不管学生的爱好是什么 } public int GetHashCode(Student obj) { return obj.Id.GetHashCode(); } }
然后调用
list.Distinct(new Compare()).ToList().ForEach(s => Console.WriteLine(s.ToString()));
执行结果:
我们按照Id去给这个集合去重成功!
3.如何编写一个具有扩展性的去重方法
调用:
list.MyDistinct(s=>s.Id).ToList().ForEach(s => Console.WriteLine(s.ToString()));
用到了泛型、委托、扩展方法等知识点。可以用于任何集合的各种去重场景
转载来源:https://www.cnblogs.com/Robert-go-go/p/5399198.html
js 日期格式转换(转载)
1.当前时间转为 “yyyy-MM-dd HH:MM:SS”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
2.任意日期转为 “yyyy-MM-dd HH:MM:SS”,将 date 变量作为参数传递。
3.任意日期转为 “yyyy-MM-dd ”,稍微改动即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
4.任意日期转为 “ HH:MM:SS ”,稍微改动即可,省略。
5. 将指定的格式转换为"yyyy-MM-dd"的格式,代码如下:
var oldTime = (new Date("2012/12/25 20:11:11")).getTime();
var curTime = new Date(oldTime).format("yyyy-MM-dd");
console.log(curTime);
6. 将 "时间戳" 转换为 "年月日" 的格式.
比如如下代码:
var da = 1402233166999;
da = new Date(da);
var year = da.getFullYear();
var month = da.getMonth()+1;
var date = da.getDate();
console.log([year,month,date].join('-'));
转载来源:https://www.cnblogs.com/tugenhua0707/p/3776808.html
树立目标,保持活力,gogogo!
C#日期转换(转载)
转载来源:https://www.cnblogs.com/johnblogs/p/5912632.html
DateTime.ToString()的各种日期格式
例:
ToString:2016/9/27 0:00:00
ToString("yyyy/MM/dd"):2016/09/27
ToString("yyyy-MM-dd"):2016-09-27
ToString("yyyy.MM.dd"):2016.09.27
ToString("dd/MM/yyyy"):27/09/2016
ToString("dd-MM-yyyy"):27-09-2016
ToString("yyyy年MM月dd日"):2016年09月27日
ToString("yyyy-MM-dd HH:mm:ss.fff")); 2016-09-27 01:02:03.001 // fff 越多精度越高
ToString("yyyy-MM-dd HH:mm:ss:ms")); 2016-09-27 01:02:03:234
详细解释:
1.y代表年份,注意是小写的y,大写的Y并不代表年份。
2.M表示月份。
3.d表示日期,注意D并不代表什么。
4.h或H表示小时,h用的是12小时制,H用的是24小时制。
5.m表示分钟。
6.s表示秒。注意S并不代表什么。
格式 |
输出 |
示例 |
年 |
||
y |
7 |
string yy = DateTime.Now.ToString("y-MM") yy="7-05" |
yy |
07 |
string yy = DateTime.Now.ToString("yy-MM") yy="07-05" |
yyy或更多的y |
1984 |
string yy = DateTime.Now.ToString("yyyy"); yy="2007" |
月 |
||
M |
5. |
string mon = DateTime.Parse("1984-05-09")ToString("yyyy-M") mon = "1984-5" |
MM |
05. |
string mon = DateTime.Parse("1984-05-09")ToString("MM") mon = "05" |
MMM |
如果是中文版的操作系统,则会输出:五月. 如果是英文操作系统,则输入月份前三个字母的简写:May |
string mon = DateTime.Parse("2006-07-01").ToString("MMM") 英文版操作系统:Jul 中文版操作系统:七月 |
MMMM或更多的M |
如果是中文版的操作系统,则会输出:五月. 如果是英文操作系统,则输入月份的全写 |
string mon = DateTime.Parse("2006-07-01").ToString("MMM") 英文版操作系统:July 中文版操作系统:七月 |
日期或星期 |
||
d |
9 |
string dd= DateTime.Parse("1984-05-09")ToString("d") dd= "9" |
dd |
09 |
string dd= DateTime.Parse("1984-05-09")ToString("dd") dd= "09" |
ddd |
如果是中文版的操作系统,则会输出星期,如星期三。. 如果是英文操作系统,则输出星期的简写:如 Wed |
string dd = DateTime.Parse("2006-07-01").ToString("ddd") 英文版操作系统:Wed 中文版操作系统:星期三 |
dddd或更多的d |
如果是中文版的操作系统,则会输出星期,如星期三。. 如果是英文操作系统,则输出星期:如 Wednesday |
string dd = DateTime.Parse("2006-07-01").ToString("dddd") 英文版操作系统:Wednesday 中文版操作系统:星期三 |
小时 |
||
h |
小时范围:1-12 |
string hh = DateTime.Now.ToString(“h”); hh = 8 |
hh或更多的h |
小时范围:1-12 |
string hh = DateTime.Now.ToString(“hh”); hh = 08 |
H |
小时范围:0-23 |
string hh = DateTime.Now.ToString(“yyyy-H”); hh = 2006-8 |
HH或更多的H |
小时范围:0-23 |
string hh = DateTime.Now.ToString(“yyyy-HH”); hh = 2006-08 string hh = DateTime.Pare(“2006-7-4 18:00:00”).ToString(“yyyy-HH”); hh = 2006-18 |
分钟 |
||
m |
6 |
string mm = DateTime.Now.ToString("yyyy-MM-dd-m"); mm = “2006-07-01-6”; |
mm或更多的m |
06 |
string mm = DateTime.Now.ToString("yyyy-MM-dd-mm"); mm = “2006-07-01-06”; |
秒 |
||
s |
6 |
string mm = DateTime.Now.ToString("yyyy-MM-dd-s"); mm = “2006-07-01-6”; |
ss或更多的s |
06 |
string mm = DateTime.Now.ToString("yyyy-MM-dd-ss"); mm = “2006-07-01-06”; |
但是
树立目标,保持活力,gogogo!
Nullable<System.DateTime>日期格式转换 (转载)
一、问题
1、html页面中时间显示出错,数据库中时间是正确的。
原因:没有把DateTime转成String类型。
2、 在C#中,发现不能直接使用ToString("yyyy-MM-dd HH:mm")这样的格式进行日期格式转换。
原因:在DB中,字段设置为Nullable<System.DateTime>,是可空的DateTime类型。
二、解决方案
if (user.BirthDate.HasValue)
{
userInfo.BirthDate = user.BirthDate.Value.ToString("yyyy-MM-dd");
}else
{
userInfo.BirthDate = string.Empty;
}
三、完整案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
转载来源:https://blog.csdn.net/baidu_34331194/article/details/68944906
树立目标,保持活力,gogogo!
Asp.Net MVC中Action跳转(转载)
首先action的跳转大致归类:
1跳转到与当前同一控制器内的action和不同控制器内的action、
2带有参数的action跳转和不带参数的action跳转。
3跳转到指定视图,不经过Controller的Action。
//跳转到当前Controller的指定Action(此处为Index),不带参数。
一、RedirectToAction("Index");
//跳转到指定Controller下的指定Action,不带阐述
二、RedirectToAction(ActionName,ControllerName)
//使用路由名称和路由值重定向到指定的路由。不带参数。
三、RedirectToRoute(new {controller="Home",action="Index"});
//使用路由名称和路由值重定向到指定的路由。带参数。
四、RedirectToRoute(new {controller="Home",action="Index", id=param});
//跳转到当前controller下的Action,可带参数。
五、Response.Redirect("Index?id=1");
//跳转到当前controller下的Action,不带参数。
六、return Redirect("Index");
//跳转到当前路径下的指定View(视图),但不经过Action方法。
七、return View("Index");
//跳转到指定路径下的指定View(视图),但不经过Action方法。这种方法是写全路径
八、return View("~/Views/Home/Index.aspx");
//跳转到当前Controller,当前Action下的View。 最常见。
九、return View();
以上并没有列举完,因为还有多种重载,可以F12进步了解。