Created
September 4, 2017 04:09
-
-
Save Dkowald/380d8b9ba3ff1818ea341877ac36aa7a to your computer and use it in GitHub Desktop.
Demo POCO Controller for ASP.NET Core v2
This file contains hidden or 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 System.Security.Claims; | |
| using System.Threading.Tasks; | |
| using Microsoft.AspNetCore.Http; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.Infrastructure; | |
| using Microsoft.AspNetCore.Mvc.ModelBinding; | |
| using Microsoft.AspNetCore.Mvc.ModelBinding.Internal; | |
| using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; | |
| using Microsoft.AspNetCore.Mvc.Routing; | |
| using Microsoft.AspNetCore.Mvc.ViewFeatures; | |
| using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal; | |
| using Microsoft.AspNetCore.Routing; | |
| using Microsoft.Extensions.DependencyInjection; | |
| namespace kwd.github.Controllers | |
| { | |
| public class PocoDemoController | |
| { | |
| protected PocoDemoController(IActionContextAccessor ctxAccessor) | |
| { | |
| //Get Action context direct from accessor | |
| var ctx = ctxAccessor.ActionContext; | |
| // Microsoft.AspNetCore.Mvc.Core / ControllerBase.cs | |
| HttpContext httpContext = ctx.HttpContext; | |
| HttpRequest request = ctx.HttpContext.Request; | |
| HttpResponse response = ctx.HttpContext.Response; | |
| RouteData routeData = ctx.RouteData; | |
| ModelStateDictionary modelState = ctx.ModelState; | |
| ControllerContext controllerContext = new ControllerContext(ctx); | |
| IModelMetadataProvider metadataProvider = | |
| httpContext.RequestServices.GetService<IModelMetadataProvider>(); | |
| IModelBinderFactory modelBinderFactory = | |
| httpContext.RequestServices.GetRequiredService<IModelBinderFactory>(); | |
| IUrlHelper url = | |
| httpContext.RequestServices.GetRequiredService<IUrlHelperFactory>() | |
| .GetUrlHelper(ctx); | |
| IObjectModelValidator objectValidator = | |
| httpContext.RequestServices.GetRequiredService<IObjectModelValidator>(); | |
| ClaimsPrincipal user = httpContext.User; | |
| //A set of methods for creating IActionResult ... | |
| //A set of methods for TryUpdateModel ... | |
| //A set of methods for TryValidateModel ... | |
| //objectValidator.Validate(ctx, null, "", new{}); | |
| // Microsoft.AspNetCore.Mvc.ViewFeatures / controller.cs | |
| ViewDataDictionary viewData = new ViewDataDictionary(metadataProvider, modelState); | |
| ITempDataDictionary tempData = | |
| httpContext.RequestServices.GetRequiredService<ITempDataDictionaryFactory>() | |
| .GetTempData(httpContext); | |
| //https://stackoverflow.com/questions/40330391/set-viewbag-property-in-the-constructor-of-a-asp-net-mvc-core-controller | |
| dynamic viewBag = new DynamicViewData(() => viewData); | |
| //A set of methods for creating ViewResult ... | |
| //A set of methods for creating PartialViewResult ... | |
| //A set of methods for creating ViewComponentResult ... | |
| //A set of methods for creating JsonResult ... | |
| //Implementations for IActionFilter | |
| //Implementation for IAsyncActionFilter | |
| //Implementation for IDisposable | |
| } | |
| public PocoDemoController(IActionContextAccessor ctxAccessor, | |
| IModelMetadataProvider modelMetaDataProvider, | |
| IModelBinderFactory modelBinderFactory, | |
| IUrlHelperFactory urlHelperFactory, | |
| IObjectModelValidator objectModelValidator, | |
| ITempDataDictionaryFactory tempDataDictionaryFactory) | |
| { | |
| //Get Action context direct from accessor | |
| var ctx = ctxAccessor.ActionContext; | |
| //Methods from Controller base | |
| // Microsoft.AspNetCore.Mvc.Core / ControllerBase.cs | |
| HttpContext httpContext = ctx.HttpContext; | |
| HttpRequest request = ctx.HttpContext.Request; | |
| HttpResponse response = ctx.HttpContext.Response; | |
| RouteData routeData = ctx.RouteData; | |
| ModelStateDictionary modelState = ctx.ModelState; | |
| ControllerContext controllerContext = new ControllerContext(ctx); | |
| IModelMetadataProvider metadataProvider = modelMetaDataProvider; | |
| //IModelBinderFactory modelBinderFactory | |
| IUrlHelper url = urlHelperFactory.GetUrlHelper(ctx); | |
| IObjectModelValidator objectValidator = objectModelValidator; | |
| ClaimsPrincipal user = httpContext.User; | |
| //A set of methods for creating IActionResult ... | |
| //A set of methods for TryUpdateModel ... | |
| //A set of methods for TryValidateModel ... | |
| // Microsoft.AspNetCore.Mvc.ViewFeatures / controller.cs | |
| ViewDataDictionary viewData = new ViewDataDictionary(metadataProvider, modelState); | |
| ITempDataDictionary tempData = tempDataDictionaryFactory.GetTempData(httpContext); | |
| //https://stackoverflow.com/questions/40330391/set-viewbag-property-in-the-constructor-of-a-asp-net-mvc-core-controller | |
| dynamic viewBag = new DynamicViewData(() => viewData); | |
| //A set of methods for creating ViewResult ... | |
| //A set of methods for creating PartialViewResult ... | |
| //A set of methods for creating ViewComponentResult ... | |
| //A set of methods for creating JsonResult ... | |
| //Implementations for IActionFilter | |
| //Implementation for IAsyncActionFilter | |
| //Implementation for IDisposable | |
| } | |
| public IActionResult Index() { return new ViewResult();} | |
| private async Task<bool> TryUpdateModel<T>(T model, | |
| ActionContext ctx, | |
| IModelMetadataProvider modelMetaDataProvider, | |
| IModelBinderFactory modelBinderFactory, | |
| IObjectModelValidator modelValidator) where T:class | |
| { | |
| var valueProvider =await CompositeValueProvider.CreateAsync(new ControllerContext(ctx)); | |
| return await ModelBindingHelper.TryUpdateModelAsync<T>( | |
| model, | |
| "", | |
| ctx, | |
| modelMetaDataProvider, | |
| modelBinderFactory, | |
| valueProvider, | |
| modelValidator); | |
| } | |
| private void TryValidateModel<T>(T model, ActionContext ctx, IObjectModelValidator modelValidator) | |
| where T:class | |
| { | |
| modelValidator.Validate(ctx, null, "", model); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment