Asp. NET notes Request, Response and Server use _ Practical skills

Source: Internet
Author: User
1. Request

Here is an example, through some of the methods of request to determine whether browsing the image is in the internal browsing, or directly by the Web site browsing or external use

Copy Code code as follows:

<%@ WebHandler language= "C #" class= "Image_test"%>

Using System;
Using System.Web;

public class Image_test:ihttphandler {

public void ProcessRequest (HttpContext context)
{
Context. Response.ContentType = "Image/jpeg";
If direct access to Urlreferrer is null, if the request is embedded in the page
Urlreferrer is the address of the page
String Picpath=httpcontext.current.server.mappath ("DSCF0738. JPG ");
using (System.Drawing.Bitmap Bitmap = new System.Drawing.Bitmap (Picpath)) {
using (System.Drawing.Graphics graphic = System.Drawing.Graphics.FromImage (bitmap))
{
But it's still too fragile, because Urlreferrer is still submitted by the client
Thunderbolt Cracked No Pear
if (context. Request.urlreferrer = null)
{
Graphic. Clear (System.Drawing.Color.White);
Graphic. DrawString ("No direct browsing of pictures", New System.Drawing.Font ("XXFarEastFont-Arial", 15),
System.Drawing.Brushes.Red, 0, 0);

}
Http://127.0.0.1:32581/WebSite_zzl01/vivideo_test/request/Request.aspx

else if (context. Request.UrlReferrer.Host!= "localhost")
{
Graphic. Clear (System.Drawing.Color.White);
Graphic. DrawString ("Picture only allowed to view inside the blog Park", New System.Drawing.Font ("Arial", 15),
System.Drawing.Brushes.Red, 0, 0);
}
Converting into streaming format output
Bitmap. Save (context. RESPONSE.OUTPUTSTREAM,SYSTEM.DRAWING.IMAGING.IMAGEFORMAT.JPEG);

}
}
}

public bool IsReusable {
get {
return false;
}
}

}

HTML

la la La.

2, Response

(1) Return stream, return to the client in the form of a stream
* Each write, to save in the cache, not directly to the browser, until the full or processing of talent sent to the browser
* Flush method, send to browser immediately

(2) Anti-hotlinking and so on: Do not go down in the ASPX write better
Context. Response.End ();

(3) ASPX and ASHX

Like output text, picture, download address is written in ashx, and HTML content is written in aspx

(4) REDIRECT Redirect

Flush instance:

Copy Code code as follows:

<%@ WebHandler language= "C #" class= "Response"%>

Using System;
Using System.Web;

public class Response:ihttphandler {

public void ProcessRequest (HttpContext context) {

If it is plain, then <br/> no effect
Context. Response.ContentType = "text/html";
Time-consuming operations
for (int i = 0; i < i++)
{
System.Threading.Thread.Sleep (500);
Context. Response.Write ("+i+" step start execution <br/>);
Adopt flush, send to the client immediately, the effect is very obvious!
Context. Response.Flush ();
}
}

public bool IsReusable {
get {
return false;
}
}

}


3, Server

(1) The difference between Server.Transfer and Response.Redirect

*transfer Access can only be an internal Web site, not an external one, while redirect May

* Transfer is the internal takeover of the site, only one HTTP request, and redirect is a few times to execute the HTTP request and in the Address bar display

* Transfer will pass all kinds of information directly and redirect will not
* Cannot redirect to ashx TRANSFO

Instance:

Copy Code code as follows:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

public partial class Vivideo_test_server_server:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
string text=request["id"];
if (Text = = "1") {
Response.Write ("one");
}
else if (text== "2") {
The requested information will be passed to the server
Server.Transfer ("DSCF0738.") JPG ");
}
else if (string. IsNullOrEmpty (text)
{
Response.Write ("Empty");
}
else {
It's not going to pick up the parameters.
Response.Redirect ("apple. jpg");
Unless I put it through to him.
Response.Redirect ("aaa.aspx?id=1");

}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.