Add a new controller to LiteBlog

I moved some of the actions in the Admin Controller to a new Settings Controller. I realized that after the new refactoring, adding a new controller class in LiteBlog is not a trivial task. This page explains how to add a new controller to LiteBlog.

Steps

  • Add a new controller class - eg. SettingsController in LiteBlog.Controllers.
namespace LiteBlog.Controllers
{
    public class SettingsController : BaseController, ISettingsController
    {
    }
}
  • Add a controller contract (call it ISettingsController) in LiteBlog.Controllers.Contracts namespace.
namespace LiteBlog.Controllers.Contracts
{
    public interface ISettingsController
    {
        ActionResult Update();
        ActionResult Update(Settings settings);
        ActionResult GetAccessCode(string code, string state);
        ActionResult Upload();
    }
}
  • Add action methods in the contract, and implement them in the concrete class. All controllers inherit from the BaseController class defined in LiteBlog.Controllers.
 [HttpPost]
        [Authorize]
        [ValidateFilter]
        [AjaxExceptionFilter]
        public ActionResult Update(Settings settings)
        {
            var oldSettings = _settingsComponent.Load();
            settings.AppKey = oldSettings.AppKey;
            settings.AppSecret = oldSettings.AppSecret;
            settings.AccessToken = oldSettings.AccessToken;
            var result = _settingsComponent.Save(settings);
            var resultModel = GetResultModel(result, null);
            return Json(resultModel);
        }
  • IoCRegistry (LiteBlog.IoC) contains registration information on how concrete classes have to initiated. Create a mapping between ISettingsController and SettingsController.
For<ISettingsController>().Use<SettingsController>();
  • For each action method, the view is created in LiteBlog assembly.
View.png
  • For each controller, create a resources file in Resources folder of LiteBlog.Controllers. For each action in the controller, the MessageHelper class (LiteBlog.Controllers.Helper) refers to the resource file (SettingsResources.resx) to get the appropriate message.
Resource.png

I hope this was useful. If you have any questions, please tweet me @vijayst.

Last edited Oct 19, 2013 at 7:02 AM by vijayst, version 3

Comments

No comments yet.