Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Restful ASP.NET MVC C# controller
[Authorize("Users")]
public class ProfileController : ResourceController
{
private readonly UserReports _userReports;
private readonly Func<UserService> _userServiceFactory;
private readonly ILogger _logger;
private readonly Func<ProfileViewModelValidator> _validatorFactory;
public ProfileController(UserReports userReports, Func<UserService> userServiceFactory, Func<ProfileViewModelValidator> validatorFactory, ILogger logger)
{
_userReports = userReports;
_logger = logger;
_userServiceFactory = userServiceFactory;
_validatorFactory = validatorFactory;
}
[Get("/accounts/profile")]
public ActionResult Get()
{
var user = _userReports.FindBy(User.Identity.Name);
if (user == null)
{
_logger.Write("Could not find user for " + User.Identity.Name);
return new NotFound();
}
var profile = DomainToPublic.Map(user, new ProfileViewModel());
return new OK(profile);
}
[Put("/accounts/profile")]
public ActionResult Put(ProfileViewModel profileViewModel)
{
var validationResult = _validatorFactory().Validate(profileViewModel);
var user = _userReports.FindBy(User.Identity.Name);
if (validationResult.IsValid)
{
_userServiceFactory().Update(user.Id, user.ETag, (dbUser) => PublicToDomain.Map(profileViewModel, dbUser));
return new OkSeeOther("?profileUpdated=true");
}
return new ForbiddenFor(string.Join(Environment.NewLine, validationResult.Errors.Select(x => x.ErrorMessage)));
}
[Authorize("Administrators")]
[Delete("/accounts/profile")]
public ActionResult Delete()
{
...
}
}
@dotnetchris

This comment has been minimized.

Copy link
Owner Author

@dotnetchris dotnetchris commented Aug 8, 2012

Common dependencies are grouped together, controllers are route drivers and delegate to services, controllers use content negotiation to respond with views/json as requested

@dotnetchris

This comment has been minimized.

Copy link
Owner Author

@dotnetchris dotnetchris commented Aug 14, 2012

Added auth attributes to show an example of sharing aspects because controller level filters are applied to actions also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.