0

I am rewriting an ASP.NET webforms app in MVC4 and was wondering how to solve the following problem. It is a multi-tenant app, so part of the URL has the tenant NAME in it:

http://mysite/tenant/controller/action

But tenant is an abbreviation representing the tenant, but I'd like to always convert that to the corresponding integer id and use that throughout the code. What is the best way to write that convert code once and have some variable/property available to all controller methods.

public class DivisionController : Controller
{
    //
    // GET: /Division/

    public ActionResult Index()
    {
        // I want this.TenantId to be available in all controller methods
        FetchDivisions(this.TenantId);
        return View();
    }

Is a base controller the best way to handle this or filters or attributes?

1 Answer 1

2

Yes a base controller will handle this just fine. If you need to perform a database lookup to convert the abbreviation to the integer value you can use the OnActionExecuting event like so:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);

    // Lookup code here.
}
Sign up to request clarification or add additional context in comments.

3 Comments

This looks like it works. Is there a "best practice" on providing the data to the child controller?
I'm not discounting the possibility of a "better practice" but I'm unaware of any. I use this approach for a number of projects.
What I meant was: Is there something better than say, ViewBag.TenantId = LookupTenantID();?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.