views:

26

answers:

2

I have multi-tenant ASP.NET MVC application which utilizes subdomains to determine the current tenant. Whether or not the domain is valid is determined via database table lookup.

Where would be the best place to have a function that checks if the domain is in the database?If the subdomain is not in the database, it should redirect to the Index action in the Error controller.

Placing the check in the Application_BeginRequest method in the Global.asax file doesn't work because a never ending redirect results.

A: 

u can subclass actionFilter attribute and override onactionExecuting method. in this method u can make any database checks and redirect the user appropriately

public class CustomActionFilterAttribute : ActionFilterAttribute
{
     public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if(DatabaseLookup)
        {
           return;
        }
        filterContext.Result = new RedirectResult("http://servername/Error");

    }

}

now u can decorate ur action methods with this custom actionfilter attribute

[CustomActionFilter]
public ActionResult mymethod()
{
    //action method goes here
}
Muhammad Adeel Zahid
I need a filter much earlier, before any controllers or actions are called.
Baddie
+1  A: 

Where would be the best place to have a function that checks if the domain is in the database?If the subdomain is not in the database, it should redirect to the Index action in the Error controller.

Placing the check in the Application_BeginRequest method in the Global.asax file doesn't work because a never ending redirect results.

That's the right place, you just need to check the request Url is not already /Error.

You might already be doing so, but I'd like to add that it seems pretty static information that you should cache instead of hitting the database for each request.

eglasius
One thing that bothered with `Application_BeginRequest` is that CSS/JS/Image links were triggering the function so I had to have a conditional to check if that file exists, if it did, the function would return. But I think this is because of Cassani WebServer.
Baddie