ashx的使用
1、必须实现IHttpHandler(如果需要使用Session,则还需要实现IRequiresSessionState接口,这只是个标记接口,实际上没有方法),完成方法public void ProcessRequest(HttpContext context)。例如提供一个文件下载:
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = ".*";
//context.Response.Charset = "utf-8";
//如果要直接打开则注释以下部分,或把"attachment"要改为“inline”
string downName = "下载时的文件名.txt";
context.Response.AddHeader("content-disposition",
string.Format("attachment; filename={0}", context.Server.UrlEncode(downName)));
//输出原文件
string srcPath = context.Server.MapPath("~/readme.txt");
context.Response.WriteFile(srcPath);
}
{
//context.Response.ContentType = ".*";
//context.Response.Charset = "utf-8";
//如果要直接打开则注释以下部分,或把"attachment"要改为“inline”
string downName = "下载时的文件名.txt";
context.Response.AddHeader("content-disposition",
string.Format("attachment; filename={0}", context.Server.UrlEncode(downName)));
//输出原文件
string srcPath = context.Server.MapPath("~/readme.txt");
context.Response.WriteFile(srcPath);
}
2、在web.config中,加入以下代码:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="ashx/IHttpHandler1.ashx" type="TestJavascript.IHttpHandler1, TestJavascript" />
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="ashx/IHttpHandler1.ashx" type="TestJavascript.IHttpHandler1, TestJavascript" />
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
</httpHandlers>
3、如果是IIS7,则Web.config中还需要加入:
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<add verb="*" name="IHttpHandler1" path="ashx/IHttpHandler1.ashx" type="TestJavascript.IHttpHandler1, TestJavascript" />
</handlers>
</system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<add verb="*" name="IHttpHandler1" path="ashx/IHttpHandler1.ashx" type="TestJavascript.IHttpHandler1, TestJavascript" />
</handlers>
</system.webServer>
4、如果是MVC,需要在Global.asax文件中忽略路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{folder}/{resource}.ashx/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
{
routes.("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{folder}/{resource}.ashx/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
接下来,就可以使用:http://localhost/ashx/IHttpHandler1.ashx来访问了