现在的位置: 首页 > 综合 > 正文

dotnet2.0的部署

2013年10月20日 ⁄ 综合 ⁄ 共 8928字 ⁄ 字号 评论关闭

Asp.net2.0中有两种编译方式:

1传统方式: 

      优点:
             1、使用简单,不需要任何操作,asp.net会自动编译。
             2、修改简单,自动进行监视并进行编译。
             3、调试简单,修改代码后刷新就能看到结果。
      缺点:
             1、不安全:对于不希望公开程序的人来说不适合。
             2、部署麻烦:需要复制大量的文件,包括源文件。
             3、第一次访问慢:因为在第一次访问页面的时候进行编译。
。net2.0文件在C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/Temporary ASP.NET Files下可以看到你的项目的名字的文件夹,打开你可以看到动态编译过的dll 以及扩展名为compiled的文件,compiled文件以xml的形式记录了页面和程序集之间的对应。

这样的结果是开发简单,部署麻烦,改善的方式有两种:

1安装vs2005sp1补丁可以回到以前的1.1的方式

2进行预编译(整站预编译)又分为原地预编译和部署预编译,以后维护的时候我可以采用ftp或者是http上传的方式不需要自己动手做文件复制工作。还没有第一次访问预编译导致速度慢的困扰,不需要删除源代码,系统已经自动删除,在部署的时候系统会自己在网站下创建一个app_offline.htm文件,这样访问网站的用户都看到“当前网站不可用”这样的友好提示。
在发布时候采用默认方式,勾选“允许更新此预编译站点”,并且不勾选“使用固定命名和单页程序集”,程序集的命名是随机的每次预编译发布都会不同。,使用这种方式发布网站,就只能更新整个站点,因此也叫整站预编译,另外两种选项,

1允许更新此预编译站点:使用这种选项,所有的aspx页面文件中不会出现任何html标签,所有内容都编译进入dll中,bin目录又多了一个xml格式的compiled 文件来指示页面文件和后台代码在编译后存在于哪个程序集中。但是页面文件却不能被删除。

2使用固定命名和单页程序集:使用这种选项,一个页面将会对应一个dll。

预编译方式的优缺点:

1安全 你的网站不可更新,即使是html代码和控件标记也看不到。

2方便:编译后没有任何源代码。

3还可以使用命令行(aspnet_compiler)来预编译,这样就可以实现每日编译或者定时编译。

缺点:部署的时候需要更新整个网站,比较慢。

不能单独编译一个文件,只能预编译站点所有文件。

 httpContent(请求上下文)

http处理程序是响应对asp.net web应用程序的请求而运行的程序.我们在开发中最常见的处理程序就是.aspx文件的asp.net页面处理程序.而在asp.net中还包含了处理处理.asmx的web服务处理程序,用于所有asp.net用户控件的默认http处理.ascx.

httpcontent 添加了一个profile对象,是2.0中添加的新对象,他获取的是在当前用户配置文件中配置的profileBase对象.

在asp.net中提供了两种方式去创建一个自定义的http处理程序,:创建一个实现httpHandler接口的类,用来创建同步处理程序,或者创建一个可实现IHttpAsyncHandler的类,用来创建异步处理程序,不管是哪种借口都要求实现IsReusable属性和ProcessRequest方法.IsReusable属性指定IHttpHandlerFactory对象是否可以将你的处理程序放置在池中,并且重新使用他们提高程序的性能,或是否在每次请求时创建新实例.

步骤1:在IIs中添加扩展名的处理

2实现IHttpHandler的类

3防盗链的效果

using System;
using System.Data;
using System.Configuration;
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;
using System.Globalization;
using System.IO;

namespace HandlerAndModule
{
    
public class ImageHandler:IHttpHandler
    {
        
#region IHttpHandler 成员

        public bool IsReusable
        {
            
get { return true; }
        }

        public void ProcessRequest(System.Web.HttpContext context)
        {
            
//获取当前Request对象
            HttpRequest req = context.Request;
            
//从Request对象中获取所请求文件的物理路径
            string IMG_Path = req.PhysicalPath;
            
string ContentType;

            //判断请求的URL是否为本站内的URL
            if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0)
            {
                
if (CultureInfo.InvariantCulture.CompareInfo.Compare(req.Url.Host, req.UrlReferrer.Host, CompareOptions.IgnoreCase) != 0)
                {
                    IMG_Path 
= context.Server.MapPath("~/images/error.gif");
                }
            }

            //根据路径名获取输出内容类型
            ContentType = GetContentType(IMG_Path);

            //判断文件是否存在
            if (File.Exists(IMG_Path))
            {
                context.Response.StatusCode 
= 200;
                context.Response.ContentType 
= ContentType;
                context.Response.WriteFile(IMG_Path);
            }
            
else
            {
                context.Response.StatusCode 
= 404;
                context.Response.Status 
= "无法找到您请求的文件";
            }
        }

        #endregion

        /// <summary>
        
/// 此方法用于从路径获取输出的MIME类型
        
/// </summary>
        
/// <param name="path">文件路径</param>
        
/// <returns>对应的MIME类型</returns>
        private string GetContentType(string path)
        {
            
//获取文件的扩展名
            string extension = Path.GetExtension(path);
            
string contentType;
            
//判断扩展名对应的Mime类型
            switch (extension)
            {
                
case ".gif":
                    contentType 
= "image/gif";
                    
break;
                
case "image/jpeg":
                    contentType 
= "image/jpeg";
                    
break;
                
case ".png":
                    contentType 
= "image/png";
                    
break;
                
default:
                    contentType 
= "";
                    
break;
            }
            
//返回mime类型
            return contentType;
        }
    }
}

4还需要在web.config中配置处理程序,以便能处理IIS中的请求.通过在应用程序中创建一个<httpHandlers>节点,即可注册HTTP处理程序,下面就是与我们这个程序相关的一些Http处理程序的配置信息:

<httpHandlers>
   <add verb="*" path="*.jpg"
                   type="HandlerAndModule.ImageHandler, HandlerAndModule "/>
   <add verb="*" path="*.gif"
      type="HandlerAndModule.ImageHandler, HandlerAndModule "/>
   <add verb="*" path="*.png"
      type="HandlerAndModule.ImageHandler, HandlerAndModule "/>
  </httpHandlers>

 verb可以是Get或者是Post,表示对Get或者是Post的请求进行处理.在Type属性中,逗号前的字符串是HttpHanddler的实现类的类名,后面的字符串就是处理程序的文件名.

假如你从网站的<img >标签中访问该图片,则将会出现图片无法访问的错误图片信息.

IHttpHandlerFactory接口用于创建和管理处理请求的HTTP处理程序.

 

using System;
using System.Data;
using System.Configuration;
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;

namespace HandlerFactory
{
    
public class RARHandler:IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {
        
#region IHttpHandler 成员

        public bool IsReusable
        {
            
get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req 
= context.Request;
            
//从Request对象中获取所请求文件的物理路径
            string RAR_Path = req.PhysicalPath;
            
//设置HTTP输出的MIME类型
            string ContentType = "application/x-tar";
            
//根据Session中UserName是否存在判断用户是否登陆
            if (context.Session["UserName"== null)
            {
                
//未登陆则设置状态代码为404
                context.Response.StatusCode = 404;
                context.Response.End();
            }
            
else
            {
                
//登陆则输出RAR文件
                context.Response.StatusCode = 200;
                context.Response.ContentType 
= ContentType;
                context.Response.WriteFile(RAR_Path);
            }
        }

        #endregion
    }
}

 

 

using System;
using System.Data;
using System.Configuration;
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;

namespace HandlerFactory
{
    
public class MyHandlerFactory:IHttpHandlerFactory
    {
        
#region IHttpHandlerFactory 成员

        IHttpHandler IHttpHandlerFactory.GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            
//判断文件扩展名
            if (url.EndsWith(".jpeg", StringComparison.InvariantCultureIgnoreCase) || url.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase))
                
//实例化并返回一个JPGHandler
                return new JPGHandler();
            
else if (url.EndsWith(".rar", StringComparison.InvariantCultureIgnoreCase))
                
//实例化并返回一个RARHandler
                return new RARHandler();
            
else
                
return null;
        }

        void IHttpHandlerFactory.ReleaseHandler(IHttpHandler handler)
        {
        }

        #endregion
    }
}

 

web.config中

<httpHandlers>
            
<add path="image/*.*" verb="*" type="HandlerFactory.MyHandlerFactory,HandlerFactory"/>
        
</httpHandlers>

 

第三: HttpModule,httpModule和Http处理程序不同,Http模块是一个在每次针对应用程序发出请求时调用的程序集,而Http处理程序则是用于处理客户端的请求.HttpModule作为Http请求管线的一部分调用,他们能够在整个请求过程中访问生命周期事件.Asp.net HttpModule是针对所有请求进行的.而Http处理程序则是针对某个或者某些特殊的请求进行处理的.HttpModule的功能十分强大,应用也十分广泛,比较常用的有Forms身份验证,缓存和会话管理等.

当客户端浏览器发起的请求到达IIs后,经过HTTP RunTime处理,创建生成请求的上下文对象,然后再将请求交由HttpModule来处理,经过一个或者多个HttpModule处理后,才会交由请求处理器程序,例如HttpHandler,*.aspx或者是*.asmx等去处理.

我们可以实现IHttpModule接口的类来创建Http模块处理程序,在web.config中注册.运行生成实例,调用Init方法,执行应用程序事件BeginRequest和EndRequest等.

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Collections;
using System.Collections.Specialized;

namespace HandlerAndModule
{
    
public class IPModule:IHttpModule
    {
        
#region IHttpModule 成员

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest 
+= new EventHandler(OnBeginRequest);
        }

        #endregion

        private void OnBeginRequest(object sender, EventArgs e)
        {
            HttpApplication app 
= sender as HttpApplication;
            
if (app != null)
            {
                
if(isBadIPAddr(app.Context))
                {
                    app.Context.Response.StatusCode 
= 404;
                    app.Context.Response.End();
                }
            }
        }

        private bool isBadIPAddr(HttpContext context)
        {
            
string ip = context.Request.UserHostAddress;
            StringDictionary sdic 
= new StringDictionary();
            sdic.Add(
"127.0.0.0""127.0.0.0");
            
if (sdic.ContainsKey(ip))
                
return true;
            
else
                
return false;
        }
    }
}

相应的web.config

<httpModules>
            
<add name="IPModule" type="HandlerAndModule.IPModule, HandlerAndModule"/>          
        
</httpModules>

name表示我们这个Http模块名,type是以逗号隔开的字符串,前面部分是IHttpModule的实现类的类名,逗号后面部分是程序集的文件名.

 

抱歉!评论已关闭.