Last active
August 29, 2015 14:01
-
-
Save wshaddix/7d26d03805dafdd976a5 to your computer and use it in GitHub Desktop.
Asp.Net MVC UI Controller Base Class
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 abstract class UiControllerBase : Controller | |
{ | |
private readonly ILog _log; | |
protected UiControllerBase(ILog log) | |
{ | |
_log = log; | |
} | |
internal string LoggedInUsername | |
{ | |
get { return HttpContext.User.Identity.Name; } | |
} | |
protected override ITempDataProvider CreateTempDataProvider() | |
{ | |
return new CookieTempDataProvider(); | |
} | |
protected T DoGetAction<T>(Action<GetResult<T>> getResultMethod) where T : ActionResult | |
{ | |
var result = new GetResult<T>(); | |
try | |
{ | |
getResultMethod(result); | |
if (!string.IsNullOrEmpty(result.Message)) | |
{ | |
SetFeedbackMessage(result.Message, result.MessageType); | |
} | |
if (result.ActionResult != null) | |
{ | |
return result.ActionResult; | |
} | |
} | |
catch (Exception ex) | |
{ | |
LogException(ex); | |
throw; | |
} | |
return result.ActionResult; | |
} | |
protected ActionResult DoPostAction<T>(T model, Action<PostResult> postAction, | |
Action<T> modelPopulationAction = null) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
if (modelPopulationAction != null) | |
{ | |
modelPopulationAction(model); | |
} | |
return View(model); | |
} | |
try | |
{ | |
var result = new PostResult(); | |
postAction(result); | |
if (!string.IsNullOrEmpty(result.Message)) | |
{ | |
SetFeedbackMessage(result.Message, result.MessageType); | |
} | |
return result.ActionResult; | |
} | |
catch (Exception ex) | |
{ | |
LogException(ex, model); | |
SetFeedbackMessage(ex.Message, FeedbackTypes.Error); | |
} | |
// If we got this far, something failed, redisplay form | |
return View(model); | |
} | |
private void LogException(Exception ex, object model = null) | |
{ | |
_log.Error(new | |
{ | |
Hostname = Environment.MachineName, | |
Model = model ?? string.Empty, | |
Exception = ex, | |
RawUrl = Request == null ? String.Empty : Request.RawUrl, | |
HttpMethod = Request == null ? String.Empty : Request.HttpMethod, | |
Version = Globals.Version | |
}); | |
} | |
private void SetFeedbackMessage(string message, string feedbackType = FeedbackTypes.Success) | |
{ | |
TempData["Message"] = message; | |
TempData["FeedbackType"] = feedbackType; | |
} | |
protected class GetResult<T> | |
{ | |
public GetResult() | |
{ | |
MessageType = FeedbackTypes.Success; | |
} | |
protected internal T ActionResult { get; set; } | |
protected internal string Message { get; set; } | |
protected internal string MessageType { get; set; } | |
protected internal string ViewName { get; set; } | |
} | |
protected class PostResult | |
{ | |
public PostResult() | |
{ | |
MessageType = FeedbackTypes.Success; | |
} | |
protected internal ActionResult ActionResult { get; set; } | |
protected internal string Message { get; set; } | |
protected internal string MessageType { get; set; } | |
protected internal void SetErrorMessage(string message) | |
{ | |
Message = message; | |
MessageType = FeedbackTypes.Error; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment