I need to programmatically determine which zone the current user is accessing a web application through, and was wondering if anyone had any guidance regarding the safest way to determine this.

I understand that you can determine the SPUrlZone from an SPAlternateUrl object, but the MSDN documentation is kind of unclear (surprise, surprise) as to how the indexer by URL works. It says:

Use an indexer to return a single item from the collection. For example, if the collection is assigned to a variable named myUrls, use myUrls[index] in C# or myUrls(index) in Visual Basic, where index is either the index number of the item in the collection or a string containing the incoming URL of the request.

I did find someone's login page code here: Incorrect authentication method when using Outlook integration

That question uses the following:

SPWebApplication app = SPContext.Current.Site.WebApplication;
SPAlternateUrl u = app.AlternateUrls[Request.Url];
SPUrlZone zone = u.UrlZone;

Is that a safe and consistent way to achieve this?

Or are there outlying conditions that could cause this not to function as expected?

Alternatively, if there are issues with the above, I was thinking this could be achieved somehow by checking properties of each SPAlternateUrl iterating through the SPWebApplication.AlternateUrls collection, but obviously that's less ideal than being able to index into the collection directly.


I haven't had an answer saying this wasn't safe, and I used the code and it seems to work fine. If anyone finds a condition where this is invalid, please post it as an answer. Until then I'm going to close this question out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.