HttpHandler 重定向
我想编写一个 HttpHandler 将流量重定向到服务器上的各个网页。 用户将输入 http://www.thisissupposetoberedirected.com/site12
并应重定向到适当的站点,在此示例站点版本 1.2 中,
我知道如何使用 ASP.NET 和 C# 进行编程,但是我似乎没有掌握有关网站管理的更详细的细节。
我怎样才能完成这件事?我应该在 web.config 中做什么?我已阅读此 msdn 页面但这没有多大帮助。
I want to write an HttpHandler to redirect traffic to various webpages on the server.
The user will type in http://www.thisissupposedtoberedirected.com/site12
and should be redirected to the appropiate site, in this example site version 1.2
I know how to program in ASP.NET and C# but I don't seem to grab the finer detail about website management.
How can I manage to get this done? What should I do in the web.config? I've read this msdn page but it isn't much help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HttpHandler 实际上是相当简单的组件。
首先,您需要创建一个继承
IHttpHandler
或IHttpAsyncHandler
的类(对于您的使用,我建议使用IHttpHandler
因为实际上没有繁重的工作)正在进行提升)。然后编译 DLL 并将其放入 Web 应用程序的 bin 文件夹中。
现在是棘手的部分。在 web.config 文件中部署 HttpHandler 很棘手,因为 IIS6、IIS7 集成模式和 IIS7 经典模式之间有所不同。最好的地方是这个 MSDN 页面:
如何:注册 HTTP 处理程序
IIS6
IIS7 经典模式
IIS7 集成模式
如您所见,每个 IIS 配置都需要在 web.config 文件的略有不同的部分中进行条目。我的建议是在每个位置添加条目,以便 IIS 配置更改不会破坏您的 HttpHandler。
HttpHandlers are actually fairly simple components.
First, you need to create a class that inherits either
IHttpHandler
orIHttpAsyncHandler
(for your use, I'd suggestIHttpHandler
since there's really no heavy lifting being done).You then compile the DLL and drop it in the bin folder of your web application.
Now the tricky part. Deploying HttpHandlers in the web.config file is tricky since it's different between IIS6, IIS7 Integrated Mode, and IIS7 Classic Mode. The best place to look is this MSDN page:
How to: Register HTTP Handlers
IIS6
IIS7 Classic Mode
IIS7 Integrated Mode
As you can see, each IIS configuration requires entries in slightly different sections of the web.config file. My suggestion would be to add entries in each location so that IIS configuration changes don't break your HttpHandler.
1)您需要创建一个实现 IHttpHandler 或 IHttpAsyncHandler 的新类(后者是当您非常擅长管理自己的线程时)。在那里创建你的逻辑。
2) 然后,修改 web.config 以便注册处理程序:
这是我的 web.config 中的示例设置 - 您的可能略有不同。
现在,您的 HttpHandler 应该已注册,并且根据您在 web.config 中注册时提供的详细信息,当您请求某些 URL 时,您将被映射到您创建的处理程序,而不是普通的 ASP.NET 页面处理程序。
此外,对于您的具体问题,我不建议编写 HttpHandler - 我只会执行 DNS 重定向,或者在 OnInit 代码中进行一些查找以检查主机 Url,如果它是在您的数据库中指定的,则您可以这样做根据配置数据重定向自己。
1) You need to create a new class that implements IHttpHandler or IHttpAsyncHandler (the latter being when you are very comfortable with managing your own threads). Create your logic there.
2) Then, modify the web.config so that you register your handler:
This is a sample setup in my web.config - yours may vary slightly.
Now, your HttpHandler should be registered and based on the details provided in your registration in the web.config, when you request certain URLs, you will be mapped to the Handler you created instead of the normal ASP.NET Page Handler.
Additionally, for your specific issue, I wouldn't recommend writing a HttpHandler - I would just do a DNS redirect, or do some lookup in your OnInit code to check the Host Url, and if it is one specified in your DB, you do the redirect yourself based on the configuration data.