.Net Core WebApi Version Info call

Date: 2018-10-25
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using WebApi.Attributes;

namespace WebApi.Controllers
{
    [ExceptionHandling]
    public class CoreController : Controller
    {
        [AllowAnonymous]
        [HttpGet]
        [Route("/")]
        public IDictionary<string, object> ApiInfo()
        {
            var info = new Dictionary<string, object>();
            var assembly = GetType().Assembly;
            var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
            info["name"] = "ApiName";
            info["description"] = "Api description";
            info["version"] = fileVersionInfo.ProductVersion;
            info["date"] = new FileInfo(fileVersionInfo.FileName).CreationTimeUtc;
            return info;
        }
    }
}

//ExceptionHandling attribute

using Domain.Models.Log;
using Domain.Ports.Drivers;
using Domain.Resolving;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.Security.Authentication;

namespace WebApi.Attributes
{
    public class ExceptionHandlingAttribute : ExceptionFilterAttribute
    {
        public override void OnException(ExceptionContext context)
        {

            var storeLogMessages = AdapterResolver.Resolve<IStoreLogMessages>();
            storeLogMessages.Store(LogLevel.Error,
                $"{context.HttpContext.Request.Method} {context.HttpContext.Request.GetUri()}", context.Exception);

            ApiError apiError;
            if (context.Exception is AuthenticationException)
            {
                apiError = new ApiError("Unauthorized Access");
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            }
            else
            {
#if !DEBUG
                var msg = "An unhandled error occurred.";                
                string stack = null;
#else
                var msg = context.Exception.GetBaseException().Message;
                string stack = context.Exception.StackTrace;
#endif

                apiError = new ApiError(msg) { Detail = stack };
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            }

            context.Result = new JsonResult(apiError);

            base.OnException(context);
        }
    }

    public class ApiException : Exception
    {
        public int StatusCode { get; set; }

        public IList<ValidationResult> Results { get; set; }

        public ApiException(string message,
            int statusCode = 500,
            IList<ValidationResult> results = null) :
            base(message)
        {
            StatusCode = statusCode;
            Results = results;
        }
        public ApiException(Exception ex, int statusCode = 500) : base(ex.Message)
        {
            StatusCode = statusCode;
        }
    }


    public class ApiError
    {
        public string Message { get; set; }
        public bool IsError { get; set; }
        public string Detail { get; set; }
        public IList<ValidationResult> Results { get; set; }

        public ApiError(string message)
        {
            Message = message;
            IsError = true;
        }

        public ApiError(ModelStateDictionary modelState)
        {
            IsError = true;
            if (modelState != null && modelState.Any(m => m.Value.Errors.Count > 0))
            {
                Message = "Please correct the specified Results and try again.";
                //Results = modelState.SelectMany(m => m.Value.Results).ToDictionary(m => m.Key, m=> m.ErrorMessage);
                //Results = modelState.SelectMany(m => m.Value.Results.Select( me => new KeyValuePair<string,string>( m.Key,me.ErrorMessage) ));
                //Results = modelState.SelectMany(m => m.Value.Results.Select(me => new ModelError { FieldName = m.Key, ErrorMessage = me.ErrorMessage }));
            }
        }
    }
}


15660cookie-check.Net Core WebApi Version Info call