IHttpHandlerFactory and URL Mapping

22 篇文章 0 订阅

There are many ways to create a complete URL Mapping solution. But most of the serious URL Mapping code, utilizes something a little more advanced than the urlMapping node in Web.Config.

The next level up from my previous post, needs to take you into the world of HTTP Handlers. I did not have all the time in the world on this post, so I have decided to put some simple elements of the solution, rather than a complete tutorial. I trust using any keywords from here, you will find lots of other resources.

This post will just simply look at a small example of the IHttpHandlerFactory interface and the Web.Config settings.

What you need to do:

1. Implement the IHttpHandlerFactory
2. Configure Web.Config with your handler


The Interface


public class DefaultPageHandler : IHttpHandlerFactory

public IHttpHandler GetHandler(HttpContext context, string requestType,
string url, string pathTranslated)  
{   }    
public void ReleaseHandler(IHttpHandler handler)   {   }  }

The Web.Config

<httpHandlers> <remove verb="POST,GET" path="*.ext"/> <add verb="POST,GET" path="*.ext" type="DefaultPageHandler"/> </httpHandlers>

Web.Config Dynamic update?

A good solution can map these up itself. Here is how you dynamically add a handler section to the Web.Config. Dynamically adding this is useful for mapping extensions, and perhaps even paths dynamically. Below is sample code:

#region write to web.config
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
 
            HttpHandlersSection section =
              (HttpHandlersSection)config.GetSection("system.web/httpHandlers");
 
            HttpHandlerAction actionAdd =
               new HttpHandlerAction("/*.ext","DefaultPageHandler","POST,GET");
           
            section.Handlers.Add(actionAdd);
           
            config.Save();
 
#endregion

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值