Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [FromHeader] Guid id
- public IModelBinder GetBinder(ModelBinderProviderContext context)
- {
- if (context == null)
- {
- throw new ArgumentNullException(nameof(context));
- }
- if (context.BindingInfo.BindingSource != null &&
- context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Header))
- {
- // We only support strings and collections of strings. Some cases can fail
- // at runtime due to collections we can't modify.
- if (context.Metadata.ModelType == typeof(string) ||
- context.Metadata.ElementType == typeof(string))
- {
- return new HeaderModelBinder();
- }
- }
- return null;
- }
- public class GuidHeaderModelBinder : IModelBinder
- {
- public Task BindModelAsync(ModelBindingContext bindingContext)
- {
- if (bindingContext.ModelType != typeof(Guid)) return Task.CompletedTask;
- if (!bindingContext.BindingSource.CanAcceptDataFrom(BindingSource.Header)) return Task.CompletedTask;
- var headerName = bindingContext.ModelName;
- var stringValue = bindingContext.HttpContext.Request.Headers[headerName];
- bindingContext.ModelState.SetModelValue(bindingContext.ModelName, stringValue, stringValue);
- // Attempt to parse the guid
- if (Guid.TryParse(stringValue, out var valueAsGuid))
- {
- bindingContext.Result = ModelBindingResult.Success(valueAsGuid);
- }
- return Task.CompletedTask;
- }
- }
- public IActionResult SampleAction(
- [FromHeader(Name = "my-guid")][ModelBinder(BinderType = typeof(GuidHeaderModelBinder))]Guid foo)
- {
- return Json(new { foo });
- }
- $.ajax({
- method: 'GET',
- headers: { 'my-guid': '70e9dfda-4982-4b88-96f9-d7d284a10cb4' },
- url: '/home/sampleaction'
- });
- public async Task BindModelAsync(ModelBindingContext BindingContext)
- {
- // Read HTTP header.
- string headerName = BindingContext.FieldName;
- if (BindingContext.HttpContext.Request.Headers.ContainsKey(headerName))
- {
- StringValues headerValues = BindingContext.HttpContext.Request.Headers[headerName];
- if (headerValues == StringValues.Empty)
- {
- // Value not found in HTTP header. Substitute empty GUID.
- BindingContext.ModelState.SetModelValue(BindingContext.FieldName, headerValues, Guid.Empty.ToString());
- BindingContext.Result = ModelBindingResult.Success(Guid.Empty);
- }
- else
- {
- // Value found in HTTP header.
- string correlationIdText = headerValues[0];
- BindingContext.ModelState.SetModelValue(BindingContext.FieldName, headerValues, correlationIdText);
- // Parse GUID.
- BindingContext.Result = Guid.TryParse(correlationIdText, out Guid correlationId)
- ? ModelBindingResult.Success(correlationId)
- : ModelBindingResult.Failed();
- }
- }
- else
- {
- // HTTP header not found.
- BindingContext.Result = ModelBindingResult.Failed();
- }
- await Task.FromResult(default(object));
- }
- public class GuidHeaderModelBinderProvider : IModelBinderProvider
- {
- public IModelBinder GetBinder(ModelBinderProviderContext Context)
- {
- if (Context.Metadata.ModelType == typeof(Guid))
- {
- if (Context.BindingInfo.BindingSource == BindingSource.Header)
- {
- return new BinderTypeModelBinder(typeof(GuidHeaderModelBinder));
- }
- }
- return null;
- }
- }
- [HttpGet("getbars")]
- public async Task<string> GetBarsAsync([FromHeader] Guid CorrelationId, int Count)
- {
- Logger.Log(CorrelationId, $"Creating {Count} foo bars.");
- StringBuilder stringBuilder = new StringBuilder();
- for (int count = 0; count < Count; count++)
- {
- stringBuilder.Append("Bar! ");
- }
- return await Task.FromResult(stringBuilder.ToString());
- }
- // Add MVC and configure model binding.
- Services.AddMvc(Options =>
- {
- Options.ModelBinderProviders.Insert(0, new GuidHeaderModelBinderProvider());
- });
Add Comment
Please, Sign In to add comment