求问
public class Handler : IHttpHandler {
public bool IsReusable {
get {
return true;
}
}
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "image/jpeg ";
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.BufferOutput = false;
int size;
switch (context.Request.QueryString[ "Size "]) {
case "S ":
size = 1;
break;
case "M ":
size = 2;
break;
case "L ":
size = 3;
break;
default:
size = 4;
break;
}
Int32 id = -1; '这里为什么为-1?
Stream stream = null;
if (context.Request.QueryString[ "PhotoID "] != null && context.Request.QueryString[ "PhotoID "] != " ") {
id = Convert.ToInt32(context.Request.QueryString[ "PhotoID "]);
stream = GetPhoto(id, size);
} else {
id = Convert.ToInt32(context.Request.QueryString[ "AlbumID "]);
stream = GetFirstPhoto(id, size);
}
’这里是说 如果photoID为空 那么调用 GetFirstPhoto方法?
if (stream == null) stream = GetPhoto(size);
const int buffersize = 1024 * 16;
byte[] buffer = new byte[buffersize];
int count = stream.Read(buffer, 0, buffersize);
while (count > 0) {
context.Response.OutputStream.Write(buffer, 0, count);
count = stream.Read(buffer, 0, buffersize); '这段什么意思?
}
}
------解决方案--------------------Int32 id = -1;
初始化id,设定为一个不可能的ID。
默认的int类型初始值是0。作为ID,0常常是有逻辑意义的,所以需要做这样的初始化,从而明白id = Convert.ToInt32(context.Request.QueryString[ "PhotoID "])这句式不是执行了。
context.Request.QueryString[ "PhotoID "] != null && context.Request.QueryString[ "PhotoID "] != " ")
这句代码很烂,可以这样写的String.NullOrEmpty(context.Request.QueryString[ "PhotoID "])。你的理解是对的。
最后这个,是把stream(也就是读到的photo)写到buffer里边去。