-
Notifications
You must be signed in to change notification settings - Fork 3
CtcApi.Web.Mvc
The CtcApi.Web.Mvc namespace includes a BaseController object that your Controllers can inherit from in place of the standard MVC Controller provided by the .NET Framework. Doing so provides additional information and functionality to your application.
When your Controller inherits from BaseController and passes a reference for the currently executing Assembly to the constructor, the ViewBag.Version property is automatically populated with your application's version.
using CtcApi.Web.Mvc;
...
namespace MyMvcProject.Controllers
{
public class MyController : BaseController
{
...
public MyController() : base(Assembly.GetExecutingAssembly())
{
...
}
}
} |
This makes it very easy to add the version information to a page, log output, etc.
<html>
...
<body>
...
<div class="footer">
...
@ViewBag.Version;
...
</div>
</body>
</html> |
One of the things that makes ASP.NET applications difficult to unit test is that the code is not being executed from a server, so there is no HttpContext present. Even in cases where the testing engine is able to execute tests within an HttpContext a lot of overhead is incurred and tests often take longer to complete. To address this, the BaseController provides a SessionWrapper property, which can be used in place of the Session object.
First, tell the application to use the CustomControllerFactory from the CtcApi when instantiating new Controllers.
using CtcApi.Web.Mvc;
...
protected void Application_Start()
{
...
// use our custom factory to instantiate controllers
ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
...
} |
Then, use SessionWrapper any place you would normally use Session.
using CtcApi.Web.Mvc;
...
public class MyController : BaseController
{
...
public ActionResult Index()
{
...
// use SessionWrapper instead of Session to ensure compatibility w/ unit tests
SessionWrapper["InitialVisit"] = DateTime.Now;
...
}
} |
Your Controllers will behave normally when run from a web server, but now have the benefit of providing a local collection object when performing unit tests - or any other situation where you want to more closely control or verify the contents of the "Session".
using CtcApi.Web.Mvc;
...
[TestClass]
public class TestMyController
{
...
[TestMethod]
public void VerifySessionVariables
{
...
IStateProvider session = new CtcApi.Web.DictionaryStateProvider();
MyController controller = new MyController();
controller.SessionWrapper = session;
...
object initialVisit = session["InitialVisit"];
Assert.IsNotInstanceOfType(initialVisit, typeof(DateTime));
}
} |
ASP.NET MVC provides AuthorizeAttribute for denoting that an Action requires authorization, and which Role(s) should be granted access. The problem with this attribute is that it only accepts hard-coded string literals for the role names. Changing the role(s) then requires modifying the code and re-releasing it.
[Authorize("Developers")]
public ActionResult Index()
{
...
}
To address this shortcoming, the CtcApi provides the additional AuthorizeFromConfigAttribute class - which functions the same as AuthorizeAttribute, but gets the role names from the appSettings key specified. This allows updating the access role(s) simply by modifying the Web.config/App.config file.
[AuthorizeFromConfig("AdminUsers")]
public ActionResult Index()
{
...
}
<configuration>
...
<appSettings>
<add key="AdminUsers" value="Developers"/>
</appSettings>
...
</configuration>
NOTE: This attribute class is essentially a wrapper around AuthorizeAttribute, so you can also specify multiple roles (separated by commas) in the .config file.
<configuration>
...
<appSettings>
<add key="AdminUsers" value="Developers,Webmasters"/>
</appSettings>
...
</configuration>