ashx学习

.net 里的ashx跟java里的servlet类似,可以不用管html之类的东西。

新建一个空的asp.net项目,不是空的也可以。

添加一个web handler codebehide:

wtf.ashx

<%@ WebHandler Language="C#" Class="testashx.wtf" %>

wtf.ashx.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.18444
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace testashx
{
	using System;
	using System.Web;
	using System.Web.UI;

	
	public class wtf : System.Web.IHttpHandler
	{
		
		public bool IsReusable {
			get {
				return false;
			}
		}

		public void ProcessRequest (HttpContext context)
		{
			context.Response.Write (typeof(wtf));
		}
	}
}

也可以不是codebehide,只要继承System.Web.IHttpHandler,如添加一个类

ta.cs

using System;
using System.Web;

namespace testashx
{

	public class ta : System.Web.IHttpHandler
	{

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}

		public void ProcessRequest(HttpContext context)
		{
			context.Response.Write("aaa");
		}
	}
}

这里测试用的是jexus,在服务器上新建部署目录/home/www/testashx,把编译的bin拷到这个目录下,把wtf.ashx也拷到这个目录下,把Default.aspx也拷到这个目录下。

来一个:

[root@centos7 ~]#curl 127.0.0.1:888888/wtf.ashx
 
testashx.wtf
[root@centos7 ~]#

可以像

curl 127.0.0.1:888888/Default.aspx
一样愉快地访问了。

如果不是codebehide,在web.config 的<HttpHandlers>里加映射,那么前面加的那个ta.cs类,就这样:

<?xml version="1.0"?>
<!--
Web.config file for testashx.

The settings that can be used in this file are documented at 
http://www.mono-project.com/Config_system.web and 
http://msdn2.microsoft.com/en-us/library/b5ysx397.aspx
-->
<configuration>
  <system.web>
<customErrors mode="Off"/>
    <compilation defaultLanguage="C#" debug="true">
      <assemblies>
      </assemblies>
    </compilation>
<!--    <customErrors mode="RemoteOnly">
    </customErrors>-->
    <authentication mode="None">
    </authentication>
    <authorization>
      <allow users="*" />
    </authorization>
    <httpHandlers>
        <add verb="*" path="/tmd" type="testashx.ta" validate="false"/>
    </httpHandlers>
    <trace enabled="false" localOnly="true" pageOutput="false" requestLimit="10" traceMode="SortByTime" />
    <sessionState mode="InProc" cookieless="false" timeout="20" />
    <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
    <pages>
    </pages>
  </system.web>
</configuration>
试下:

curl 127.0.0.1:888888/tmd
返回:

[root@centos7 ~]# curl 127.0.0.1:888888/tmd
aaa
[root@centos7 ~]# 
所以,ashx文件不是必须的。


最后/home/www/testashx有这些

[root@centos7 tashx]# ll
total 12
drwxr-xr-x. 2 root root   25 Sep 12 21:55 bin
-rw-r--r--. 1 root root  393 Sep 12 21:11 Default.aspx
-rw-r--r--. 1 root root 1192 Sep 12 22:08 web.config
-rw-r--r--. 1 root root   57 Sep 12 21:41 wtf.ashx
[root@centos7 tashx]# 

bin下有这些

[root@centos7 tashx]# ll bin
total 8
-rw-r--r--. 1 root root 4608 Sep 12 21:54 testashx.dll
[root@centos7 tashx]# 

哦,jexus配置是这样的:

[root@centos7 jexus]# pwd
/usr/jexus
[root@centos7 jexus]# ls
def.py            jwsHttpd.exe.so  jxHost2.dll     state2.conf
jws               jwsLog.exe       jxHost2.dll.so  state4.conf
jws.conf          jwsState.exe     jxHost.dll
jws.exe           jxAspx2.dll      jxHost.dll.so
jwsHttpd2.exe     jxAspx2.dll.so   log
jwsHttpd2.exe.so  jxAspx.dll       os.def
cron        jwsHttpd.exe      jxAspx.dll.so    siteconf
[root@centos7 jexus]# cd siteconf/
[root@centos7 siteconf]# ls
default   tashx
[root@centos7 siteconf]# cat tashx
######################
# Web Site: CM_MC_UNIVERSAL 
########################################

port=28888
root=/ /home/www/tashx
hosts=*


# addr=0.0.0.0
# CheckQuery=false
# NoLog=true
# NoFile=/index.aspx
# Keep_Alive=false
# UseGZIP=true
# UseHttps=true
# DenyFrom=192.168.0.233, 192.168.1.*, 192.168.2.0/24
# AllowFrom=192.168.*.*
# DenyDirs=~/cgi, ~/upfiles
# indexes=myindex.aspx
# rewrite=^/.+?\.(asp|php|cgi|pl|sh)$ /index.aspx

# reproxy=/bbs/ http://192.168.1.112/bbs/

# Jexus php fastcgi address is '/var/run/jexus/phpsvr'
#######################################################
# fastcgi.add=php|socket:/var/run/jexus/phpsvr

# php-fpm listen address is '127.0.0.1:9000'
############################################
# fastcgi.add=php|tcp:127.0.0.1:9000


[root@centos7 siteconf]#

哦,添加完项目jexus 的host文件后,要重启jexus,就是这个:

[root@centos7 siteconf]# ll /usr/jexus/siteconf/tashx
-rw-r--r--. 1 root root 767 Sep 12 21:20 /usr/jexus/siteconf/tashx
[root@centos7 siteconf]#

重启:

[root@centos7 jexus]# ./jws
Usage: jws {start|stop|restart|regsvr|status|-v}
[root@centos7 jexus]# pwd
/usr/jexus
[root@centos7 jexus]# ./jws restart
Restarting ... OK
[root@centos7 jexus]#










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值