Last active
October 10, 2016 22:11
-
-
Save stevenknox/1e515a4fdb7edd748e7cfa1cb0f190e4 to your computer and use it in GitHub Desktop.
UseTenant TypeFilterAttribute for SaaSKit - https://github.com/saaskit/saaskit/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UseTenantAttribute : TypeFilterAttribute | |
{ | |
public UseTenantAttribute() : base(typeof(UseTenantFilter)) | |
{ | |
} | |
private class UseTenantFilter : IActionFilter | |
{ | |
readonly AppTenant _tenant; | |
public UseTenantFilter(ITenant<AppTenant> tenant) | |
{ | |
this._tenant = tenant.Value; | |
} | |
public void OnActionExecuting(ActionExecutingContext context) | |
{ | |
if (context.Controller is ITenantController) | |
{ | |
((ITenantController)context.Controller).Tenant = _tenant; | |
} | |
else | |
{ | |
context.Controller.ViewBag.Tenant = _tenant; | |
} | |
} | |
public void OnActionExecuted(ActionExecutedContext context) | |
{ | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ITenantController | |
{ | |
IAppTenant Tenant { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using standard controller and returning Tenant within ViewBag | |
public class TestActionController : Controller | |
{ | |
[UseTenant] | |
public IActionResult Index() | |
{ | |
AppTenant tenant = (AppTenant)ViewBag.Tenant; | |
return View(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Using ITenantController interface and setting AppTenant property | |
[UseTenant] | |
public class TestController : Controller, ITenantController | |
{ | |
public AppTenant Tenant { get; set; } | |
public IActionResult Index() | |
{ | |
ViewBag.TenantName = Tenant.Name; | |
return View(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment