HttpHandler 重定向

发布于 2024-08-30 09:00:28 字数 360 浏览 8 评论 0原文

我想编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-09-06 09:00:28

HttpHandler 实际上是相当简单的组件。

首先,您需要创建一个继承 IHttpHandlerIHttpAsyncHandler 的类(对于您的使用,我建议使用 IHttpHandler 因为实际上没有繁重的工作)正在进行提升)。

然后编译 DLL 并将其放入 Web 应用程序的 bin 文件夹中。

现在是棘手的部分。在 web.config 文件中部署 HttpHandler 很棘手,因为 IIS6、IIS7 集成模式和 IIS7 经典模式之间有所不同。最好的地方是这个 MSDN 页面:

如何:注册 HTTP 处理程序

IIS6

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" />
    </httpHandlers>
  <system.web>
</configuration>

IIS7 经典模式

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" />
    </httpHandlers>
  <system.web>
  <system.webServer>
    <add name=SampleHandler" verb="*" path="SampleHandler.new" 
      Modules="IsapiModule" 
      scriptProcessor="FrameworkPath\aspnet_isapi.dll"
      resourceType="File" />
  </system.webServer>
</configuration>

IIS7 集成模式

<configuration>
  <system.webServer>
    <handlers>
      <add name="SampleHandler" verb="*" 
        path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" 
        resourceType="Unspecified" />
    </handlers>
  <system.webServer>
</configuration>

如您所见,每个 IIS 配置都需要在 web.config 文件的略有不同的部分中进行条目。我的建议是在每个位置添加条目,以便 IIS 配置更改不会破坏您的 HttpHandler。

HttpHandlers are actually fairly simple components.

First, you need to create a class that inherits either IHttpHandler or IHttpAsyncHandler (for your use, I'd suggest IHttpHandler 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

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" />
    </httpHandlers>
  <system.web>
</configuration>

IIS7 Classic Mode

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" />
    </httpHandlers>
  <system.web>
  <system.webServer>
    <add name=SampleHandler" verb="*" path="SampleHandler.new" 
      Modules="IsapiModule" 
      scriptProcessor="FrameworkPath\aspnet_isapi.dll"
      resourceType="File" />
  </system.webServer>
</configuration>

IIS7 Integrated Mode

<configuration>
  <system.webServer>
    <handlers>
      <add name="SampleHandler" verb="*" 
        path="SampleHandler.new" 
        type="SampleHandler, SampleHandlerAssembly" 
        resourceType="Unspecified" />
    </handlers>
  <system.webServer>
</configuration>

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.

瀞厅☆埖开 2024-09-06 09:00:28

1)您需要创建一个实现 IHttpHandler 或 IHttpAsyncHandler 的新类(后者是当您非常擅长管理自己的线程时)。在那里创建你的逻辑。

2) 然后,修改 web.config 以便注册处理程序:

<system.web>
    <httpHandlers>
      <add verb="*" path="*.htm" type="System.Web.StaticFileHandler"/>
      <add verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
      <add verb="*" path="*.ico" type="System.Web.StaticFileHandler"/>
    </httpHandlers>
</system.web>

这是我的 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:

<system.web>
    <httpHandlers>
      <add verb="*" path="*.htm" type="System.Web.StaticFileHandler"/>
      <add verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
      <add verb="*" path="*.ico" type="System.Web.StaticFileHandler"/>
    </httpHandlers>
</system.web>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文