一、 通过自定义的HttpModule和HttpHandler,重写url,自定义路由规则,实现 Web API功能。 简单说

就是  请求路径 例如 service/method, 那么就指向当前应用app下某个services的某个方法method。

首先,实现IHttpModule,对于请求路径 符合自定义规则的,交给自定义的HttpHandler

public class UrlRoutingModule : IHttpModule
{
//其他成员
public RouteCollection RouteCollection { get; set; }
public void Init(HttpApplication context)
{
context.PostResolveRequestCache += new EventHandler(this.OnApplicationPostResolveRequestCache); } private void OnApplicationPostResolveRequestCache(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
string path = context.Request.AppRelativeCurrentExecutionFilePath; //为了解决iis6 中framework4.0 与 framework2.0共存的问题
if (path.EndsWith("/eurl.axd"))
{
path = path.Substring(0, path.Length - 9);
} string[] pathArray = path.Split('/');
string fileName = pathArray[pathArray.Length - 1]; if (fileName.IndexOf(".") > 0)
{
context.RemapHandler(HttpContext.Current.Handler);
}
else
{
PageRouteHandler handler = new PageRouteHandler();
context.RemapHandler(handler);
}
} #region IHttpModule 成员 public void Dispose()
{ } #endregion
}

  

自定义的httphandler 实现 IHttpHandler接口

  public class PageRouteHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; } } public void ProcessRequest(HttpContext context)
{
string result = string.Empty;
string path = context.Request.AppRelativeCurrentExecutionFilePath;
string[] pathArray = path.Split('/');
Func<HttpContext, string> handler = null; string serviceName = string.Empty;
string methodName = string.Empty;
if (pathArray.Length >= 3)
{
serviceName = pathArray[1];
methodName = pathArray[2];
} if (!string.IsNullOrEmpty(serviceName) && !string.IsNullOrEmpty(methodName))
{
handler = GetWebMethod(serviceName + "." + methodName);
if (handler != null)
{
result = handler(context);
}
}
if (handler == null)
{
result = "{\"result\":\"not exist handler service\"}"; } if (context.Request.AcceptTypes != null && context.Request.AcceptTypes.Contains("application/json"))
{
context.Response.ContentType = "application/json";
} string callback = context.Request["jsoncallback"];
if (!string.IsNullOrEmpty(callback))
{
result = callback + "(" + result + ")";
context.Response.AddHeader("Access-Control-Allow-Origin", "*");
} context.Response.Write(result); }
}

  

自定义的 pageroutehandler的作用,就是处理合法请求时,通过委托方式调用请求的方法,并把结果返回。 获取委托时使用了缓存,getwebmethod 代码如下

   public static Dictionary<string, Func<HttpContext, string>> WebMethods
{
get
{
Dictionary<string, Func<HttpContext, string>> _webMethods = HttpRuntime.Cache["WebMethods"] as Dictionary<string, Func<HttpContext, string>>;
if (_webMethods == null)
{
_webMethods = new Dictionary<string, Func<HttpContext, string>>();
}
return _webMethods;
}
} public static Func<HttpContext, string> GetWebMethod(string classMethodName)
{
if (string.IsNullOrEmpty(classMethodName))
{
return null;
}
string[] arrClassMethod = classMethodName.Split('.');
if (arrClassMethod.Length != 2)
{
return null;
} Func<HttpContext, string> handler = null;
if (!WebMethods.ContainsKey(classMethodName))
{ string binPath = AppDomain.CurrentDomain.RelativeSearchPath;
foreach (string dll in Directory.GetFiles(binPath))
{
FileInfo fi = new FileInfo(dll);
if (fi.Extension.Equals(".dll", StringComparison.CurrentCultureIgnoreCase) && !fi.Name.StartsWith("system.") && !fi.Name.StartsWith("microsoft."))
{
Assembly assembly = Assembly.LoadFile(dll);
string typeName = assembly.GetName().Name + "." + arrClassMethod[0];
Type type = assembly.GetType(typeName, false, true);
if (type != null)
{
handler = (Func<HttpContext, string>)Delegate.CreateDelegate(typeof(Func<HttpContext, string>), type.GetMethod(arrClassMethod[1], BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Static, null, new Type[1] { typeof(HttpContext) }, null));
WebMethods.Add(classMethodName, handler);
break;
}
}
} }
else
{
handler = WebMethods[classMethodName];
}
return handler;
}

  

二  使用规则。首先在web config 的<httpModules>加上<add name="RouteModule" type="LinWebAPI.UrlRoutingModule, LinWebAPI"/>(这里type就是 你自己定义的httpmodule的类的全名了)。

只要你把你的后台类暴露出来给前台调用,定义方法符合    public static string  method(HttpContext context)   这个规则即可。

假设 你有个模块的类 TestServices  下有个 方法  public  static  string TestMethod(HttpContext context), 那么前端页面 通过 ajax请求路径为  Testservices/testmethod,

至于请求的参数,你可以通过?a=x&b=y这种方式加到请求路径后面,也可以放通过post方式提交  例如  jquery的 $.ajax(url,{a:x,b:y}....。

而对于 方法 TestMethod(HttpContext context),要获取请求的参数,直接从 string a = context.Request["a"]  即可。

***

对于iis6,要支持这种不带扩展名的请求路径,需要

1. 打开 IIS Microsoft 管理控制台 (MMC),右键单击本地计算机名称,然后单击“属性”。
2. 单击“MIME 类型”。
3. 单击“新建”。
4. 在“扩展名”框中,键入星号 (*)。
5. 在“MIME 类型”框中,键入 application/octet-stream。

对于iis7,要支持这种不带扩展名的请求路径,需要

1 网站托管模式为 经典模式
2 处理程序映射--》添加脚本映射--》配置(请求路径:* ;可执行文件:C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll)

最精简的自定义.net 开发框架的更多相关文章

  1. HTC one/M7电信802d 毒蛇ViperOne2.1.0/高级毒蛇工具/完美root,精简/更多自定义,稳定,流畅ROM

    ROM版本 HTC One/M7 802d ROM作者 雪狼团队·大盛 http://weibo.com/DaShengdd Android版本 Android 4.2.2 创建日期 2013.09. ...

  2. 精简版自定义 jquery

    function $(id) { var el = 'string' == typeof id ? document.getElementById(id) : id; el.on = function ...

  3. 可靠通信的保障 —— 使用ACK机制发送自定义信息——ESFramework 通信框架4.0 快速上手(12)

    使用ESPlus.Application.CustomizeInfo.Passive.ICustomizeInfoOutter接口的Send方法,我们已经可以给服务端或其它在线客户端发送自定义信息了, ...

  4. 一个自定义python分布式专用爬虫框架。支持断点爬取和确保消息100%不丢失,哪怕是在爬取进行中随意关停和随意对电脑断电。

    0.此框架只能用于爬虫,由框架来调度url请求,必须按照此方式开发,没有做到类似celery的通用分布式功能,也不方便测试.可以使用另外一个,基于函数式编程的,调度一切函数的分布式框架,做到了兼容任何 ...

  5. RESTful登录设计(基于Spring及Redis的Token鉴权)

    转载自:http://www.scienjus.com/restful-token-authorization/ http://m.blog.csdn.net/article/details?id=4 ...

  6. App后台开发运维和架构实践学习总结(1)——App后台核心技术之用户验证方案

    对于初学者来说,对Token和Session的使用难免会限于困境,开发过程中知道有这个东西,但却不知道为什么要用他?更不知道其原理,今天我就带大家一起分析分析这东西. 一.使用Token进行身份鉴权 ...

  7. HikariCP 连接最快的连接池

    三点原因 1.字节码精简 2.自定义 FastList 代替ArrayList ;避免每次get()调用都要进行range check,避免调用remove()时的从头到尾的扫描: 3.优化代码和拦截 ...

  8. nodejs的精简型和全栈型开发框架介绍

    总体来说你可以将Node.js开发框架归结为两类: - 精简型框架 - 全栈型框架 下面我们就对这两种框架进行探讨. 精简型框架 精简型框架提供的是最基本的功能和APIs,这类框架本身就是被设计成用来 ...

  9. 干货——基于Nop的精简版开发框架(附源码)

    .NET的开发人员应该都知道这个大名鼎鼎的高质量b2c开源项目-nopCommerce,基于EntityFramework和MVC开发,拥有透明且结构良好的解决方案,同时结合了开源和商业软件的最佳特性 ...

随机推荐

  1. ArrayList集合方法

  2. 短信验证登陆-中国网建提供的SMS短信平台

    一.JAVA发送手机短信常见的有三种方式(如下所列): 使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 使用短信mao的方式进行短信 ...

  3. MySQL 学习资料

    MySQL 学习资料: MySQL 学习资料 网址 MySQL 教程(菜鸟教程) http://www.runoob.com/mysql/mysql-tutorial.html MySQL 教程(极客 ...

  4. MongoDB 设置账号和密码

    一.安装MongoDB 1.环境配置: i.操作系统:CentOS release 6.8 (Final) [root@iZ2ze2pbbffhmn53ao4tuaZ bin]# cat /etc/r ...

  5. SDRAM单字写操作

    SDRAM单字写操作 1.单字写操作时序 2.写verilog程序体会 在初始状态,先写好跳转条件.If()....else... 3.通过仿顺序操作来实现连续写操作 首先完成单字写操作,然后跳转到下 ...

  6. 银行卡所属公司判断 参考自https://blog.csdn.net/well2049/article/details/79429130

    在网上找到了一个银行卡的验证,通过阿里的支付宝接口进行校验,能够准确识别是否存在,归属行,卡号类型是储蓄卡(DC)还是信用卡(CC). 接口api:需要传入的2个参数,卡号cardNo和cardBin ...

  7. go bytes缓冲区使用介绍 -转自https://www.cnblogs.com/--xiaoyao--/p/5122138.html

    缓冲区原理简介: go字节缓冲区底层以字节切片做存储,切片存在长度len与容量cap, 缓冲区写从长度len的位置开始写,当len>cap时,会自动扩容.缓冲区读会从内置标记off位置开始读(o ...

  8. Python打包文件夹的方法小结(zip,tar,tar.gz等)

    本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...

  9. Hiero扩展工具包开发小结

    写了两个月,Hiero扩展工具包终于完成了,包括了7个扩展内容,从Tags的扩展到TranscodeImage任务的检查再到版本的搜索,还有新Token的创建,算是对Hiero原生程序做了一个补充,提 ...

  10. 自定义Write节点的afterrender属性

    由于nuke中的write节点提供了beforerender,afterrender这类事件,我们想添加一些功能只需要在这里面敲入代码即可.事件一旦发生,自然会触发我们敲入的co de.   Nuke ...