惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 廖勇军

关于c++的头文件依赖 增强资源管理器右键功能,含源代码 VC中结构体的内存布局 进程间共享句柄三种方式 SocanCode连接Oracle的方法 SocanCode7之模板编写 不用再纠结反射影响效率了 javascript总结 IIS7.0中使用MVC3,静态页正常,其它404 一起来灭掉IE6! iis express感觉还不错 关于sqlite使用entity framework的布署问题 - 廖勇军 - 博客园 负margin实现div的左右排版 原来Jquery.load的方法可以一直load下去 错误1067进程意外终止 关于省市联动的问题想法 javac编译多个带package文件 远程服务器返回了错误 NOTFOUND Java程序放到Linux上出现的问题
ashx的使用
廖勇军 · 2011-11-07 · via 博客园 - 廖勇军

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);
        }

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>

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>

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
            );
        }

接下来,就可以使用:http://localhost/ashx/IHttpHandler1.ashx来访问了