ASP.NET08

配置错误页

1
2
3
4
5
6
7
8
9
10
11
<customErrors mode="On" defaultRedirect="~/test2/404.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
---------------------------在Application_Error中发生错误时转向另外一个页面------------------------
protected void Application_Error(object sender, EventArgs e)
{
string errorMsg = Context.Error.Message + Environment.NewLine + Context.Error.StackTrace + "【" + DateTime.Now.ToString() + "】" + Environment.NewLine + "====================================================" + Environment.NewLine;
File.AppendAllText(Server.MapPath("~/log.txt"), errorMsg);
Response.Redirect("~/test2/404.htm");
}

mvc中只能将静态页面放在根目录或者创建一个Error控制器

ASP.NET06

httprequest一些成员

  • Request.UrlReferrer获取请求该资源的原地址

    用处:判断host是否为本站

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    public void ProcessRequest(HttpContext context)
    {
    var uri = context.Request.UrlReferrer;
    if (uri == null || uri.Host != "localhost")
    {
    context.Response.ContentType = "text/plain";
    context.Response.Write("forbid");
    context.Response.End();
    }
    context.Response.ContentType = "image/jpg";
    context.Response.WriteFile("536896a693f71.jpg");
    }

    只有在img请求和localhost下才能访问

  • Request.UserHostAddress获得访问者的IP地址

ASP.NET05

创建缩略图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var file = context.Request.Files[0]; 
//从上传图片创建大图
using (var bigImage = Image.FromStream(file.InputStream))
{
//等比创建小图
using (var smallImage = new Bitmap(200, 200 * bigImage.Height / bigImage.Width))
{
//创建画布将大图画入小图
using (var graphics = Graphics.FromImage(smallImage))
{
graphics.DrawImage(bigImage, 0, 0, smallImage.Width, smallImage.Height);
}
//保存
bigImage.Save(context.Server.MapPath(Guid.NewGuid() + "_big.jpg"));
smallImage.Save(context.Server.MapPath(Guid.NewGuid() + "_small.jpg"));
}
}

ASP.NET04

文件上传

html中添加表单

1
2
3
4
<form method="post" enctype="multipart/form-data" action="ProcessUpload.ashx">
<input type="file" name="file1"/>
<input type="submit" value="upload"/>
</form>

文件上传注意点:

  1. 必须使用post
  2. form中enctype="multipart/form-data",使用表单快
  3. 添加文件域 <input type="file" name="file1"/>

ASP.NET03

ASP.NET简单三层步骤

  1. 建立好相应的文件夹
文件夹 用处
CURD.BLL CURD的业务逻辑层
CURD.DAL CURD的数据访问层
CURD.Model CURD的传输层模型(本次将数据库模型与dto混为一谈)
CURD.UI CURD的网页端

ASP.NET01

输入域名发送过程

  1. 输入域名后,浏览器会先发送给DNS解析得到IP地址
  2. 浏览器封装http报文发送服务器
  3. 服务器根据报文,返回相对应的(静态,动态)网页

静态页面:在服务器就相当于直接读取文件字符串然后返回客户端浏览器;(任何时候访问看到的都是一样的界面。)

细说 ASP.NET Cache 及其高级用法

许多做过程序性能优化的人,或者关注过程程序性能的人,应该都使用过各类缓存技术。 而我今天所说的Cache是专指ASP.NET的Cache,我们可以使用HttpRuntime.Cache访问到的那个Cache,而不是其它的缓存技术。

以前我在【我心目中的Asp.net核心对象】 这篇博客中简单地提过它,今天我打算为它写篇专题博客,专门来谈谈它,因为它实在是太重要了。在这篇博客中, 我不仅要介绍它的一些常见用法,还将介绍它的一些高级用法。 在上篇博客【在.net中读写config文件的各种方法】 的结尾处,我给大家留了一个问题,今天,我将在这篇博客中给出一个我认为较为完美的答案。

本文提到的【延迟操作】方法(如:延迟合并写入数据库)属于我的经验总结,希望大家能喜欢这个思路。