{"id":1566,"date":"2018-10-25T11:24:38","date_gmt":"2018-10-25T10:24:38","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=1566"},"modified":"2020-10-15T08:02:10","modified_gmt":"2020-10-15T07:02:10","slug":"net-core-webapi-version-info-call","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/net-core-webapi-version-info-call\/","title":{"rendered":".Net Core WebApi Version Info call"},"content":{"rendered":"\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using Microsoft.AspNetCore.Authorization;\nusing Microsoft.AspNetCore.Mvc;\nusing System.Collections.Generic;\nusing System.Diagnostics;\nusing System.IO;\nusing WebApi.Attributes;\n\nnamespace WebApi.Controllers\n{\n    [ExceptionHandling]\n    public class CoreController : Controller\n    {\n        [AllowAnonymous]\n        [HttpGet]\n        [Route(\"\/\")]\n        public IDictionary&lt;string, object> ApiInfo()\n        {\n            var info = new Dictionary&lt;string, object>();\n            var assembly = GetType().Assembly;\n            var fileVersionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);\n            info[\"name\"] = \"ApiName\";\n            info[\"description\"] = \"Api description\";\n            info[\"version\"] = fileVersionInfo.ProductVersion;\n            info[\"date\"] = new FileInfo(fileVersionInfo.FileName).CreationTimeUtc;\n            return info;\n        }\n    }\n}\n\n\/\/ExceptionHandling attribute\n\nusing Domain.Models.Log;\nusing Domain.Ports.Drivers;\nusing Domain.Resolving;\nusing Microsoft.ApplicationInsights.AspNetCore.Extensions;\nusing Microsoft.AspNetCore.Mvc;\nusing Microsoft.AspNetCore.Mvc.Filters;\nusing Microsoft.AspNetCore.Mvc.ModelBinding;\nusing System;\nusing System.Collections.Generic;\nusing System.ComponentModel.DataAnnotations;\nusing System.Linq;\nusing System.Net;\nusing System.Security.Authentication;\n\nnamespace WebApi.Attributes\n{\n    public class ExceptionHandlingAttribute : ExceptionFilterAttribute\n    {\n        public override void OnException(ExceptionContext context)\n        {\n\n            var storeLogMessages = AdapterResolver.Resolve&lt;IStoreLogMessages>();\n            storeLogMessages.Store(LogLevel.Error,\n                $\"{context.HttpContext.Request.Method} {context.HttpContext.Request.GetUri()}\", context.Exception);\n\n            ApiError apiError;\n            if (context.Exception is AuthenticationException)\n            {\n                apiError = new ApiError(\"Unauthorized Access\");\n                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;\n            }\n            else\n            {\n#if !DEBUG\n                var msg = \"An unhandled error occurred.\";                \n                string stack = null;\n#else\n                var msg = context.Exception.GetBaseException().Message;\n                string stack = context.Exception.StackTrace;\n#endif\n\n                apiError = new ApiError(msg) { Detail = stack };\n                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;\n            }\n\n            context.Result = new JsonResult(apiError);\n\n            base.OnException(context);\n        }\n    }\n\n    public class ApiException : Exception\n    {\n        public int StatusCode { get; set; }\n\n        public IList&lt;ValidationResult> Results { get; set; }\n\n        public ApiException(string message,\n            int statusCode = 500,\n            IList&lt;ValidationResult> results = null) :\n            base(message)\n        {\n            StatusCode = statusCode;\n            Results = results;\n        }\n        public ApiException(Exception ex, int statusCode = 500) : base(ex.Message)\n        {\n            StatusCode = statusCode;\n        }\n    }\n\n\n    public class ApiError\n    {\n        public string Message { get; set; }\n        public bool IsError { get; set; }\n        public string Detail { get; set; }\n        public IList&lt;ValidationResult> Results { get; set; }\n\n        public ApiError(string message)\n        {\n            Message = message;\n            IsError = true;\n        }\n\n        public ApiError(ModelStateDictionary modelState)\n        {\n            IsError = true;\n            if (modelState != null &amp;&amp; modelState.Any(m => m.Value.Errors.Count > 0))\n            {\n                Message = \"Please correct the specified Results and try again.\";\n                \/\/Results = modelState.SelectMany(m => m.Value.Results).ToDictionary(m => m.Key, m=> m.ErrorMessage);\n                \/\/Results = modelState.SelectMany(m => m.Value.Results.Select( me => new KeyValuePair&lt;string,string>( m.Key,me.ErrorMessage) ));\n                \/\/Results = modelState.SelectMany(m => m.Value.Results.Select(me => new ModelError { FieldName = m.Key, ErrorMessage = me.ErrorMessage }));\n            }\n        }\n    }\n}\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1566","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=1566"}],"version-history":[{"count":2,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1566\/revisions"}],"predecessor-version":[{"id":4149,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/1566\/revisions\/4149"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=1566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=1566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=1566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}