一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Image img = Image.FromStream(ms); ms.Close(); this.pictureBox1.Image 二. C#中byte[]与string的转换代码 1、System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] inputBytes =converter.GetBytes(inputString); string inputString = converter.GetString(inputBytes); 2、string inputString = System.Convert.ToBase64String(inputBytes); byte[] inputBytes = System.Convert.FromBase64String(inputString); FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); 三. C# Stream 和 byte[] 之间的转换 /// 将 Stream 转成 byte[] public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 stream.Seek(0, SeekOrigin.Begin); return bytes; } /// 将 byte[] 转成 Stream public Stream BytesToStream(byte[] bytes) { Stream stream = new […]
View Details
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
//直接方法重载+匿名对象 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); //构造路由然后添加 Route myroute = new Route("{controller}/{action}", new MvcRouteHandler()); routes.Add("MyRoute0", myroute); //跨命名空间路由 routes.MapRoute( "AddContollerRoute", "Home/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "URLsAndRoutes.AdditionalControllers" } ); routes.MapRoute( "MyRoute1", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new[] { "URLsAndRoutes.Controllers" } ); //可变长度路由 + 正则表达式匹配路由 routes.MapRoute( "MyRoute2", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "^Index$|^About$" }, new[] { "URLsAndRoutes.Controllers" } ); //指定请求方法 routes.MapRoute("MyRoute3", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET") }, new[] { "URLsAndRoutes.Controllers" } ); |
先来看下面两个个url,对比一下: http://xxx.yyy.com/Admin/UserManager.aspx http://xxx.yyy.com/Admin/DeleteUser/1001 对于第1个Url,假设它与服务器上的文件有直接的关系,那么服务器在接受客户端请求并将对应的文件传送给客户端。我们大概可以猜到它是对用户管理的一个页面,它的物理文件UserManager.aspx在网站根目录下面的Admin文件夹中。而第2个url,在不知道Mvc路由以及Url重写时,很难猜到这个Url背后具体有些什么,前提条件是基于.Net框架开发的Web项目。 那么在这里,我们引入Asp.Net Mvc Url路由这个概念,也正是本文所要阐述的主题,Url路由模块是负责映射从浏览器请求到特定的控制器动作。 基于上面提到的Url路由以及其作用,我们就大概能猜到第2个Url背后有些啥了。 自然而然的Admin就是控制器了,DeleteUser是控制器里面的动作及Action了,1001就是Action的参数。到这里我们对Url路由有一个简单的认识,那么接着看下面一组url,假设Home是控制器,Index是控制器里面的Action,至于1001或者2345这类数据我们暂且约定为参数: http://xxx.yyy.com http://xxx.yyy.com/Home/1001 http://xxx.yyy.com/Index/1001 http://xxx.yyy.com/Home/Index/1001/2345 http://xxx.yyy.com/System/Home/Index/1001 按照约定,从上面的几组Url中可以看出,有的缺控制器,有的缺Action,有的带有好几个参数,有的又莫名的多出了控制器、Action、参数之外的东西,那么他们能正确的访问吗? 注册路由 在vs2012里面新建一个asp.net mvc4 web 应用程序项目,可以在项目的根目录下App_Start里面看到RouteConfig文件,在这个文件里面就可以添加url路由了。在文件里面可以看到MapRoute 这个方法,它是RouteCollection的扩展方法,并且含有多个重载,下面看看MapRoute方法的参数,这里选参数最多的那个:
1 |
1 |
public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces); |
从上面的代码片段大概可以看出该方法含有路由名称、路由的Url、默认值、约束、首先查找路由所在的命名空间,该方法是返回对映射路由的引用。不过在RouteConfig文件中我们可以看到添加路由的代码:
1 |
1 2 3 4 5 |
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); |
稍微分析一下这段代码可以知道,在路由集合里面添加了一个名为"Default"的路由,并且有一个默认的路由指向,指向名称为Home的控制器,并且Action为Index,可以明显的看到这里 "id = UrlParameter.Optional" 的写法,它的意思就是说Action的参数可以不需要用户来指定,可以缺省。 多个参数如何传递 之前在一个QQ群里面见到一兄弟在问,类似这样的Url“http://xxx.yyy.com/Home/Index/1001/2345/tauruswu”在路由里面怎么配置?其实这个也很简单,我们只需要将路由配置、以及Action稍作调整。
1 |
1 2 3 4 5 |
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}/{*catchall}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); |
这个是调整之后的路由配置,只需要在Url后面再加上"{*catchall}"即可,那么Action调整之后如下
1 |
1 2 3 4 5 6 |
public ActionResult Index(string id,string catchall) { ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; return View(); } |
在Action里面定义了一个参数"catchall"用来接收Url中除了Id之外其他所有的参数值,那么上面那个Url中接收的参数就是“2345/tauruswu”,这样看起来好不好了,不过我觉得怪怪的,有没有更好的解决方法了?Url有必要写成那么长吗?这个问题在后续文章中会涉及到。 你也许会犯的错误 不知各位兄弟在刚刚接触MVC的时候,有没有碰到过这样的问题,如下图 那么这个错误是如何引起的了?代码是这么写的
1 |
1 2 3 4 5 6 7 8 9 10 11 12 |
namespace MvcDebug.Controllers.Tauruswu { public class HomeController : Controller { public ActionResult Index(string id, string catchall) { ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; return View(); } } } |
我们再看英文提示大概就是说匹配出了多个控制器为"Home"的类型,在它的提示中也告诉了我们解决方法,说在MapRoute方法中使用"namespaces"这个参数,我们先将这个放在一边,将抛出这个错误的源码给找出来,具体的源码在DefaultControllerFactory这个类中,看类名就能猜出它的作用是什么了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<span style="font-family:'sans serif', tahoma, verdana, helvetica;line-height:1.5;"> <pre class="prettyprint lang-cs">private Type GetControllerTypeWithinNamespaces(RouteBase route, string controllerName, HashSet<string> namespaces) { // Once the master list of controllers has been created we can quickly index into it ControllerTypeCache.EnsureInitialized(BuildManager); ICollection<Type> matchingTypes = ControllerTypeCache.GetControllerTypes(controllerName, namespaces); switch (matchingTypes.Count) { case 0: // no matching types return null; case 1: // single matching type return matchingTypes.First(); default: // multiple matching types throw CreateAmbiguousControllerException(route, controllerName, matchingTypes); } } |
1 2 |
<span style="font-family:'sans serif', tahoma, verdana, helvetica;line-height:1.5;">上面粗体标出的代码,因为我们在路由中没有配置“namespaces”这个参数,这段代码的意思就是通过控制器名称获取所匹配的控制器类型集合,当获取到集合数据之后就开始Case了,很明显,这里集合的数目是2,自然就抛错了。 问题出来了,该如何解决?在错误提示中说要用到“namespaces”这个参数,我们能不能告诉MVC解析引擎,在解析控制器名称时,能不能对某些命名空间进行优先处理,事实上是可以的,只需要在配置路由的地方稍微调整一下</span> |
1 2 3 4 |
<pre class="prettyprint lang-cs">routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}/{*catchall}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "MvcDebug.Controllers.Tauruswu" } ); |
上面标粗的代码的意思是说优先解析“MvcDebug.Controllers.Tauruswu”这个命名空间里面的控制器。 路由约束 对于路由约束,我个人觉得这个可能在实际开发中用的不是很多,既然MapRoute方法提供了constraints这个参数及约束,那么在某些特定的场合肯定能发挥它的作用。在MVC中提供了三种路由约束方案,分别是: 1)正则表达式 ,2)http方法 ,3)自定义约束 。下面我们分别介绍下这三种约束的使用方法。 1)正则表达式 ,在路由配置中,我们做了这样的规则,只匹配Controller名称以H开头的
1 2 3 4 5 6 7 |
<pre class="prettyprint lang-cs">routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}/{*catchall}", defaults: new { controller = "Demo", action = "Index", id = UrlParameter.Optional }, constraints: new { controller = "^H.*" }, namespaces: new[] { "MvcDebug.Controllers.Tauruswu" } ); |
2) http方法 ,我们将路由配置稍作修改
1 2 3 4 5 6 7 |
<pre class="prettyprint lang-cs">routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}/{*catchall}", defaults: new { controller = "Demo", action = "Index", id = UrlParameter.Optional }, constraints: new { httpMethod = new HttpMethodConstraint("POST"), }, namespaces: new[] { "MvcDebug.Controllers.Tauruswu" } ); |
然后在对应的Action上面打个标记
1 2 3 4 5 6 7 |
<pre class="prettyprint lang-cs">[HttpGet] public ActionResult Index() { ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。"; return View(); } |
你们说这样行不行了? 3) 自定义约束,如果说上面两种需求还是不能满足你,那么我们可以自定义约束。我们翻看HttpMethodConstraint这个类,可以看到它是继承IRouteConstraint这个接口,其定义是
1 2 3 4 |
<pre class="prettyprint lang-cs">public interface IRouteConstraint { bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection); } |
里面只有一个布尔类型的方法,关于这个例子,是从网上借鉴过来的,如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<pre class="prettyprint lang-cs">public class CustomConstraint : IRouteConstraint { private string requiredAgent; public CustomConstraint(string agentArgs) { this.requiredAgent = agentArgs; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return httpContext.Request.UserAgent != null && httpContext.Request.UserAgent.Contains(requiredAgent); } } |
这段代码的意思就是检查客户端请求的UserAgent属性值,看它是否含有一个被传递给构造函数的值。那么我们将路由作如下修改
1 2 3 4 5 6 7 |
<pre class="prettyprint lang-cs">routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}/{*catchall}", defaults: new { controller = "Demo", action = "Index", id = UrlParameter.Optional }, constraints: new { customConstraint = new CustomConstraint("IE"), }, namespaces: new[] { "MvcDebug.Controllers.Tauruswu" } ); |
很显然,这个只能在IE游览器下面游览。 如何创建自定义路由处理程序 在翻阅MapRoute方法源码的时候,看到了这么一段
1 2 3 4 5 6 7 8 9 |
<pre class="prettyprint lang-cs">Route route = new Route(url, new MvcRouteHandler()) { Defaults = CreateRouteValueDictionary(defaults), Constraints = CreateRouteValueDictionary(constraints), DataTokens = new RouteValueDictionary() }; ..... routes.Add(name, route); |
在Route实例化的时候,它是用到了“MvcRouteHandler“这个类,该类继承”IRouteHandler“接口,如果我们不用系统里面已经定义好的路由处理方案,我们要自己来实现一套?改怎么下手,此时只需要继承”IRouteHandler“这个接口并实现”GetHttpHandler“方法即可。 最后在添加路由时,像这样操作
1 |
routes.Add(new Route("DemoUrl",new DemoRouteHandler()); |
当我们在游览器里面请求/DemoUrl这个地址时,就会用到我们自定义的处理程序,在实际开发当中,如果真的要用到自定义路由处理程序,那么我们就要实现很多原本框架所实现的空能,虽然这给我们带来了很大的扩展空间,但是又不可控。 总结 Url路由系统是通过请求地址进行解析从而得到以目标Controller/Action名称为核心的路由数据,Url路由系统是建立在Asp.net 之上,我们在调试System.Web.Routing的源码时候可以得知。在这里我们由浅入深的了解了路由系统,接下来我们会讲到控制器以及Action,也是最为核心的东西。 转发处:http://www.cnblogs.com/wucj/p/3113878.html
View Details之前还以为从上至下统一用上UTF-8就高枕无忧了,哪知道今天在抓取新浪微博的数据的时候还是遇到字符的异常。 从新浪微博抓到的数据在入库的时候抛出异常: Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90…' 发现导致异常的字符不是繁体而是某种佛经文字。。。额滴神。。。但是按道理UTF-8应该能支持才对啊,他不是万能的么? 原来问题出在mysql上,mysql如果设置编码集为utf8那么它最多只能支持到3个字节的UTF-8编码,而4个字节的UTF-8字符还是存在的,这样一来如果你建表的时候用的utf8字符集出异常就理所当然了。 解决方法很简单,修改字段或者表的字符集为utf8mb4。 比较蛋疼的是,字符集utf8mb4在mysql 5.5.3之后才支持。。。 [2015.03.30]更新: 事实证明只做上面的步骤是无法解决问题的,如果需要解决请参考:http://info.michael-simons.eu/2013/01/21/java-mysql-and-multi-byte-utf-8-support/ 明天要研究如何升级mysql了。。。 参考: http://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc http://topic.csdn.net/u/20091107/17/c0eb2463-b4bb-4197-bd67-0459db8aa137.html from:http://blog.csdn.net/shootyou/article/details/8236024
View DetailsMysql数据库上,执行Sql语句: INSERT tn_Areas (AreaCode, ParentCode, Name, PostCode, DisplayOrder, Depth, ChildCount) VALUES ('41030400', '41030000', '瀍河回族区', ", 1557, 3, 0); 报错: [Err] 1366 – Incorrect string value: '\xE7\x80\x8D\xE6\xB2\xB3…' for column 'Name' at row 1 原因:数据库的字符集是gb2312,不能识别繁体字 解决办法:修改数据库字符集为utf8 from:http://c.jinhusns.com/u/lix/b-438
View Details在写LINQ语句的时候,往往会看到.AsEnumerable() 和 .AsQueryable() 。 例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
string strcon = "Data Source=.\\SQLEXPRESS;Initial Catalog=Db_Example;Persist Security Info=True;User ID=sa;Password=sa"; SqlConnection con = new SqlConnection(strcon); con.Open(); string strsql = "select * from SC,Course where SC.Cno=Course.Cno"; SqlDataAdapter da = new SqlDataAdapter(strsql,con); DataSet ds = new DataSet(); da.Fill(ds, "mytable"); DataTable tables=ds.Tables["mytable"]; //创建表 var dslp = from d in tables<strong>.AsEnumerable()</strong> select d;//执行LINQ语句,这里的.AsEnumerable()是延迟发生,不会立即执行,实际上什么都没有发生 foreach(var res in dslp) { Response.Write(res.Field<string>("Cname").ToString()); } |
上述代码使用LINQ 针对数据集中的数据进行筛选和整理,同样能够以一种面向对象的思想进行数据集中数据的筛选。在使用LINQ 进行数据集操作时,LINQ 不能直接从数据集对象中查询,因为数据集对象不支持LINQ 查询,所以需要使用AsEnumerable 方法返回一个泛型的对象以支持LINQ 的查询操作。 .AsEnumerable()是延迟执行的,实际上什么都没有发生,当真正使用对象的时候(例如调用:First, Single, ToList….的时候)才执行。 下面就是.AsEnumerable()与相对应的.AsQueryable()的区别: AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中。 AsQueryable将一个序列向下转换为一个IQueryable, 它生成了一个本地查询的IQueryable包装。 .AsEnumerable()延迟执行,不会立即执行。当你调用.AsEnumerable()的时候,实际上什么都没有发生。 .ToList()立即执行 当你需要操作结果的时候,用.ToList(),否则,如果仅仅是用来查询不需要进一步使用结果集,并可以延迟执行,就用.AsEnumerable()/IEnumerable /IQueryable .AsEnumerable()虽然延迟执行,但还是访问数据库,而.ToList()直接取得结果放在内存中。比如我们需要显示两个部门的员工时,部门可以先取出放置在List中,然后再依次取出各个部门的员工,这时访问的效率要高一些,因为不需要每次都访问数据库去取出部门。 IQueryable实现了IEnumberable接口。但IEnumerable<T> 换成IQueryable<T>后速度提高很多。原因: IQueryable接口与IEnumberable接口的区别: IEnumerable<T> 泛型类在调用自己的SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了,而IQueryable<T> 是将Skip ,take 这些方法表达式翻译成T-SQL语句之后再向SQL服务器发送命令,它并不是把所有数据都加载到内存里来才进行条件过滤。 IEnumerable跑的是Linq to Object,强制从数据库中读取所有数据到内存先。 from:http://www.cnblogs.com/jianglan/archive/2011/08/11/2135023.html
View Details说明: 请求验证过程检测到有潜在危险的客户端输入值,对请求的处理已经中止。 该值可能指示存在危及应用程序安全的尝试,如跨站点脚本攻击。若要允许页面重写应用程序请求验证设置, 请将 httpRuntime 配置节中的 requestValidationMode 特性设置为 requestValidationMode="2.0"。 示例: <httpRuntime requestValidationMode="2.0" />。设置此值后,可通过在 Page 指令或 <pages> 配置节中设置 validateRequest="false" 禁用请求验证。但是,在这种情况下,强烈建议应用程序显式检查所有输入。 有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkId=153133。 异常详细信息: System.Web.HttpRequestValidationException: 从客户端(myname="<p>test</p>")中检测到有潜在危险的 Request.Form 值。 如果一定要输入含标记的内容,解决方法: (1)修改web.config <system.web> ….. <httpRuntime requestValidationMode="2.0" /> </system.web> (2)Controller中添加[ValidateInput(false)] 例如: [ValidateInput(false)] public ActionResult SendData() { …. } from:http://blog.csdn.net/lanse_my/article/details/38023955
View Details【分部视图】 ASP.NET MVC 里的部分视图,相当于 Web Form 里的 User Control。我们的页面往往会有许多重用的地方,可以进行封装重用。使用 部分视图 : 1. 可以简写代码。2. 页面代码更加清晰、更好维护。 【如何使用】 在视图里有多种方法可以 加载部分视图,包括: Partial() Action() RenderPartial() RenderAction() RenderPage() 方法。 以下是这些方法的差别: Partial 与 RenderPartial 方法 1. Razor 语法:@Html.Partial() 与 @{Html.RenderPartial();} 2. 区别:Partial 可以直接输出内容,它内部是 将 html 内容转换为 string 字符(MVCHtmlString),然后缓存起来, 最后在一次性输出到页面。显然,这个转换的过程,会降低效率,所以通常使用 RenderPartial 代替。 RenderPartial 与 RenderAction 方法 1. Razor 语法:@{Html.RenderPartial();} 与 @{Html.RenderAction();} 2. 区别:RenderPartial 不需要创建 Controller 的 Action ,而 RenderAction 需要在 Controller 创建要加载的 Action。 RenderAction 会先去调用 Contorller 的 Action ,最后再 呈现视图,所以这里 页面会在 发起一个链接。 如果这个部分视图只是一些简单 的 html 代码,请使用 RenderPartial。 但如果这个部分视图 除了有 html 代码外, 还需要 通过 读取数据库里的数据 来渲染,就必须使用 RenderAction […]
View Details1:添加记录后,如何获取新添加的ID的值 比如,一个实体 TestEntity 对应一个表TestEntity(ID主键自增,Name,age),使用linq to ef 添加一条记录后,如何获取新记录的ID值?如下代码:
1 |
var te = new TestEntity () { Name = "名字", Age = 21 }; using (EFDbContext context = new EFDbContext()) { context.TestEntity .Add(te); context.SaveChanges(); return te.ID; } |
调用SaveChanges()之后,ef.ID的值就是数据库中新加记录对应自增标识列的值。Linq to ef智能地判断出ID就是自增主键标识列。 他给我们返回了。 2:列名叫“ID”的列,它不是自增列,linq to ef不让插入的问题 如标题,就是,列名叫“ID”的列,它不是主键,也不是主键,linq to ef不让插入。我已经给ID赋值了 但它一直提示 ID不能为NULL ,打断点,看了,也有值! 代码走到SaveChanges(),就报异常,提示ID不能为空!超蛋疼… 原因:默认情况,linq to ef认为只要实体类中有ID属性,数据库对应的是一定是自增标识列。 解决方式: 1)第一步 因为EFDbContex继承自Context类,所以,我们需要从新对它这个叫OnModelCreating的虚函数进行实现
1 |
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<TestEntity>().Property(p => p.ID) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); } |
2)第二步 在实体类叫ID的属性上加标记实现(记得添加引用):
1 |
public class TestEntity { [DatabaseGenerated(DatabaseGeneratedOption.None)] public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } } |
from:http://www.ithao123.cn/content-2766171.html
View Details准备一些测试数据,如下: use Test Create table Student( ID int identity(1,1) primary key, [Name] nvarchar(50) not null ) Create Table Book( ID int identity(1,1) primary key, [Name] nvarchar(50)not null, StudentID int not null ) insert into Student values('张三') insert into Student values('李四') insert into Student values('王五') select * from student --张三借的书 insert into Book values('红楼',1) insert into Book values('大话红楼',1) --李四借的书 insert into Book values('三国',2) --王五没借书 --一本错误的记录 insert into Book values('错误时怎样练成的',111) --左连接 select s.name,b.name from student as s left join Book as b on s.id=b.studentid --右连接 select s.name,b.name from student […]
View Detailsusing System; using System.Data; using System.Configuration; using System.Collections; using System.Drawing; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class study_CheckCode2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string code = CreateVerifyCode(); //取随机码 CreateImageOnPage(code, this.Context); // 输出图片 Response.Cookies.Add(new HttpCookie("CheckCode", code.ToUpper()));// 使用Cookies取验证码的值 } #region 验证码长度(默认4个验证码的长度) int length = 4; public int Length { get { return length; } set { length = value; } } #endregion […]
View Details