如何使用Global.asax文件将用户重定向? [英] How to use Global.asax file to redirect the user?

查看:168
本文介绍了如何使用Global.asax文件将用户重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我希望用他们的ISO codeS用户重定向应该怎么办?

What should be done if I want to redirect a user based on their ISO Codes?

和应该怎么检测网站用户的IS code摆在首位?就像我知道有,我们用它来检测客户端IP和所有(HTTP_FORWARDED_FOR,REMOTE_ADDRESS等),但这些信息的服务器变量
关于他们上我看过很多网站已经让我感到困惑使用什么。就像有人说有可能是通过HTTP_FORWARDED_X进出这些IP地址的返回逗号分隔的IP地址,一个是不知道哪个是真正的IP,哪些是代理地址。此外,在返回的所有IP地址可以是假的。

And how should I detect a website user's IS Code in the first place? Like I know there are these server variables that we use to detect Client IP and all (HTTP_FORWARDED_FOR, REMOTE_ADDRESS, etc) but information about them on the many sites that I have read has got me confused about what to use. Like some say there can be comma separated IPs returned by HTTP_FORWARDED_X and out of those IPs , one is not sure which is the real IP and which are the proxy addresses. Also, that all the IP addresses in returned can be fake.

所以我的问题是: -

So my questions are:-


  1. 如何获取我一个人的真实IP地址?就像我只想知道一个特定的人是坐在哪个国家这样我就可以相应地将用户重定向。

  1. How to I fetch the REAL IP Address of a person? Like I only want to know what country a particular person is sitting so I can redirect the user accordingly.

其次可以说,一个人坐在美国那么我希望用户被重定向到www.mysite.us。
如果一个人正在访问德国的我的网站,www.mysite.de是我想要的人被重定向到的地址。

Secondly lets say a person is sitting in US then I want the user to be redirected to www.mysite.us. If a person is visiting my site from Germany, www.mysite.de is the address that I want the person to be redirected to.

此外,如果DE是ISO code,那么我希望我的GermanMaster.master页面加载,如果其在美国的话,我希望是,USMaster.master应该加载。

Also if DE is the ISO Code then I want my GermanMaster.master page to load and if its US then I want that the USMaster.master should load.

那么,如何获取用户的不是假的IP地址,将用户重定向基于ISO code,然后加载母版页根据ISO code。

So how do I fetch a user's not fake IP address, redirect a user based on that ISO code and then load a master page according to the ISO Code.

我要如何做呢?我还没有与之前的Global.asax无能所以如何去了解这一切工作过。任何帮助将大大AP preciated。谢谢你。

How do I go about it? I haven't ever worked with Global.asax before so clueless how to go about it all. Any help will be greatly appreciated. Thanks.

推荐答案

我不能完全肯定的IP地址定位服务,但如果你要动态地加载基于一些标准的特定母版,那么你会希望使用 Page_ preINIT 事件在页面的生命周期。

I'm not entirely sure about the IP address location services, but if you want to dynamically load a specific MasterPage based on some criteria, then you'll want to use the Page_PreInit event in the page's life cycle.

当然,网页需要一个内容页面,使用在 @Page 指令引用一个母版。这不要紧,你在 @Page 指令使用的母版,只要你将它的存在,表明该页面是一个内容页。在后面(或基类)的code,类似下面将工作:

Of course, the page needs to be a Content page, with a reference to a MasterPage in the @Page directive. It doesn't matter which MasterPage you use in the @Page directive, just as long as it's there to indicate that the page is a Content page. In the code behind (or in a base class), something like the following would work:

C#

void Page_PreInit(Object sender, EventArgs e) {
    this.MasterPageFile = "~/NewMaster.master";
}

VB

Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
    Me.MasterPageFile = "~/NewMaster.master"
End Sub

您只想把任何你想要的逻辑,这个方法来选择正确的母版。关键是,你必须让 preINIT 事件中的变化。更改后的母版不允许该事件和ASP.NET将抛出一个讨厌的错误。

You would just put whatever logic you want in this method to pick the correct MasterPage. The key is that you have to make the change in the PreInit event. Changing the MasterPage after that event is not allowed and ASP.NET will throw a nasty error.

进行了扩展,这一点,我实现了利用在session_start 方法来执行逻辑来确定当前的母版的技术,放置文件的名称为在会话。在 preINIT 方法会那么简单地从会话负载母版文件名,而不必考虑哪些之一来使用。 (这是可能更好地使用的String.Format ,但在这个例子中,我只是串联值。)您可能还需要验证会议(母版)实际上已经改变了的MasterPageFile 物业前值(甚至是一个的有效的值)。如果将其更改为不存在的文件,事情可能会变得有点难看。

As an extension to this, I've implemented a technique that utilizes the Session_Start method to execute the logic to determine the current MasterPage, placing the name of the file into the Session. The PreInit method would then simply load the MasterPage file name from the Session rather than having to think about which one to use. (It's probably better to use String.Format, but for this example I'm just concatenating the values.) You might also want to verify that Session("MasterPage") actually has a value (and perhaps even a valid value) before changing the MasterPageFile property. Things can get a bit ugly if you change it to a file that doesn't exist.

C#

void Page_PreInit(Object sender, EventArgs e) {
    this.MasterPageFile = "~/MasterPages/" + Session("MasterPage") + ".master";
}

VB

Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
    Me.MasterPageFile = "~/MasterPages/" & Session("MasterPage") & ".master"
End Sub

您可能会发现这个页面上一些有用的信息,的与ASP.NET母版页编程,这是工作,我抓住了第一个code的例子。

You might find some useful information on this page, Working with ASP.NET Master Pages Programmatically, which is where I grabbed the first code examples.

这篇关于如何使用Global.asax文件将用户重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆