Omar
Omar

Reputation: 40182

Check conditions on each page request

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.

Upvotes: 0

Views: 362

Answers (2)

eglasius
eglasius

Reputation: 36037

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.

Upvotes: 1

Muhammad Adeel Zahid
Muhammad Adeel Zahid

Reputation: 17784

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
}

Upvotes: 0

Related Questions