c# - 使用 HttpHandler 重定向子文件夹

标签 c# asp.net httphandler

我的网站 asp 网页位于 www.mydomain.com,博客位于 www.mydomain.com/blog。我必须将博客移至子域,因此我需要编写一个模块,将对博客内任何页面的任何调用重定向到新的子域。

它在我的机器上有效,但当我上传到共享主机时则无效。任何想法出了什么问题吗?

我写了下面的代码

    public void ProcessRequest(HttpContext context)
{
    string sourceUrl =  @"www.mydomain.com/blog";// @"localhost:51393/blog"
    string destinationUrl = @"blog.mydomain.com/blog"; 
    string currentLocation = context.Request.Url.AbsoluteUri;
   if(currentLocation.ToLower().Contains(sourceUrl))
   {
       System.Web.HttpContext.Current.Response.Clear();
       System.Web.HttpContext.Current.Response.StatusCode = 301;
       System.Web.HttpContext.Current.Response.AddHeader("Location", currentLocation.Replace(sourceUrl, destinationUrl));
       System.Web.HttpContext.Current.Response.End();           
   }
}

并添加了这些处理程序

 <httpHandlers>
  <add verb="*" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
  <add verb="*" path="*" type="MyHandler,App_Code.dll"/>

非常感谢任何帮助。

我知道它与这个非常相似httpHandler - subfolder issue但没有成功。

最佳答案

只需使用 IIS 重写

下面是一个类似的代码,将 1 个 URL 重定向到另一个 URL。稍微调整一下就可以了。我通过我的 smarterasp.net 托管帐户得到了这个工作。

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

关于c# - 使用 HttpHandler 重定向子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12488709/

相关文章:

c# - MVC4 发布的应用程序帐户登录页面未加载

c# - 有什么方法可以在 asp.net mvc 5 中使用 TagHelpers 吗?

c# - 对 "copy"列表所做的更改反射(reflect)了原始列表 - c#

jQuery 发布。无法使用自定义 httphandler 获取请求参数

c# - 如何排除与 ASP.Net 中 HttpHandler 的指定路径匹配的内容?

c# - 如何在 wpf 中显示用户对按钮单击的控制?

javascript - 在链接按钮的顶部显示工具提示

c# - 如何结束用户 session 并确保用户已注销?

c# - 我应该把一个被多次调用的函数放在哪里?

asp.net-mvc - ASP.NET MVC 服务器端文件上传进度计算器