eyeballpaul
eyeballpaul

Reputation: 1735

MVC find out if current web request is for an controller/action or a static resource

I have developed an MVC web application, and for every request that comes in I need to change the culture. I have the following:

/// <summary>
/// Called from every HTTP request
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event args</param>
protected void Application_BeginRequest(object sender, EventArgs e)
{
    this.SetCulture();
}

Now the setting of the culture works perfectly, however it runs through this piece of code for EVERY request, including calls for javascript files, css files, images, fonts etc. I only want this method running for actions.

Is there anyway at this stage of a request (i.e. Application_BeginRequest) to figure out if it is a controller/action request or a static resource request?

Upvotes: 3

Views: 645

Answers (1)

Typo Johnson
Typo Johnson

Reputation: 6044

Derive all your controllers from a BaseController (which itself derives from Controller) and put your code in the OnActionExecuting function in your base controller.

public BaseController : Controller
{
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
                SetCulture();
        }
}

Upvotes: 1

Related Questions