void Execute(HttpContext context) {
//根据请求的Url,通过反射取得处理该请求的类
string url = context.Request.Url.AbsolutePath;
string[] array = url.Split(
new char[] {
'/' }, StringSplitOptions.RemoveEmptyEntries);
string typename =
"Biz";
for (
int x =
1; x < array.Length -
1; x++) {
typename +=
"." + array[x];
}
Type type =
this.GetType().Assembly.GetType(typename,
false,
true);
if (type !=
null) {
//取得类的无参数构造函数
var constructor = type.GetConstructor(
new Type[
0]);
//调用构造函数取得类的实例
var obj = constructor.Invoke(
null);
//查找请求的方法
var method = type.GetMethod(System.IO.Path.GetFileNameWithoutExtension(url));
if (method !=
null) {
var parameters = method.GetParameters();
object[] args =
null;
if (parameters.Length >
0) {
args =
new object[parameters.Length];
for (
int x =
0; x < parameters.Length; x++) {
var parameterAttr = (Attribute.GetCustomAttribute(parameters[x],
typeof(ParameterAttribute))
as ParameterAttribute) ??
new FormAttribute();
parameterAttr.Name = parameterAttr.Name ?? parameters[x].Name;
args[x] = parameterAttr.GetValue(context, parameters[x].ParameterType);
}
}
//执行方法并输出响应结果
context.Response.Write(method.Invoke(obj, args));
}
}
}