IIS根据浏览器信息中的区域设置重定向到URL

时间:2017-02-02 19:48:20

标签: asp.net iis-7

我希望IIS根据我从浏览器获得的区域设置重定向到URL。我读过有关重写表的内容,但我认为这不是解决该问题的正确方法。这个功能的配置应该只在IIS内部,但我愿意接受其他建议。

1 个答案:

答案 0 :(得分:0)

创建一个实现IHttpModule的类,然后根据您收到的Request.UserLanguages值重定向:

public class MyModule : IHttpModule{
  public void Init(HttpApplication application){
      application.BeginRequest += 
        (new EventHandler(this.Application_BeginRequest));
    application.EndRequest += 
        (new EventHandler(this.Application_EndRequest));
}

private void Application_BeginRequest(Object source, 
     EventArgs e)
{
    HttpApplication application = (HttpApplication)source;
    HttpContext context = application.Context;
    var userLanguages = context.Request.UserLanguages;
   // Do something here based on the user languages
 }

private void Application_EndRequest(Object source, EventArgs e)
{}
}

然后在web.config中注册

<configuration>
  <system.web>
<httpModules><add name="MyModule" type="MyModule"/></httpModules>
  </system.web>
</configuration>