public WebApiBuilder CreateApiController(ProjectBuilder proj, FeatureBuilder feature) { var builder = new WebApiBuilder(proj, feature); Builders.Add(builder); return(builder); }
public static void ConfigureContainer() { WebApiBuilder builder = new WebApiBuilder(); builder.RegisterApiControllers(GetExecutingAssembly()); Container = builder.Build(); Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(Container); }
private FileResult Build(WebApiBuilder builder) { var result = new FileResult() { Path = GetFilePath(builder), Contents = GetClassContents(builder) }; return(result); }
private string GetClassContents(WebApiBuilder builder) { var Namespace = builder.GetNamespace(); var inheritsText = string.IsNullOrWhiteSpace(builder.BaseTypeName) ? " : MyControllerBase" : $" : {builder.BaseTypeName}"; var methodsCode = GetMethodsContent(builder); var fieldsText = GetFieldsText(builder); var constructorText = GetConstructorsText(builder); var usingStatements = GetUsingStatements(builder); var additionalUsings = GetAdditionalUsings(builder); var allUsingStatements = usingStatements.Concat(additionalUsings) .Concat(new [] { "using System;" }) .Distinct() .OrderBy(z => z) .ToList(); var usingText = string.Join(Environment.NewLine, allUsingStatements); var overrideBasePath = string.IsNullOrWhiteSpace(builder.BasePath) ? string.Empty : $"[Route(\"{builder.BasePath})]"; var classContents = $@" //WebApiBuilder {usingText} namespace {Namespace} {{ {overrideBasePath} public class {builder.Name}{inheritsText} {{ {fieldsText} {constructorText} {methodsCode} }} }}"; return(CustomCodeFormattingEngine.Format(classContents)); }
/// <summary>Registers core components of web api infrastructure.</summary> /// <param name="services">Service collection for storing dependencies.</param> /// <param name="setup">Web api configuration options delegate.</param> /// <returns>Returns Web api builder instance.</returns> public static IWebApiBuilder AddWebApi(this IServiceCollection services, Action <WebApiConfigurationOptions> setup) { services.AddOptions(); var configurationOptions = new WebApiConfigurationOptions(); setup?.Invoke(configurationOptions); services.AddMvcCore(opt => { opt.Filters.Add(configurationOptions.ExceptionHandlerType); opt.AllowEmptyInputInBodyModelBinding = true; var readerFactory = services.BuildServiceProvider().GetRequiredService <IHttpRequestStreamReaderFactory>(); opt.ModelBinderProviders.Insert(0, new IdentityModelBinderProvider(opt.InputFormatters, readerFactory)); opt.Conventions.Insert(0, new RouteConvention(new RouteAttribute("api/v{version:apiVersion}"))); configurationOptions.SetCustomMvcOptions(opt); }) .AddJsonFormatters() .AddJsonOptions(opt => { opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); opt.SerializerSettings.Converters.Add(new StringEnumConverter() { NamingStrategy = new CamelCaseNamingStrategy() }); configurationOptions.SetCustomMvcJsonOptions(opt); }) .AddFluentValidation(o => { o.LocalizationEnabled = false; // Gets assemblies in domain which starts with modules prefix and has fluent validator classes var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(w => w.GetName() .Name.StartsWith(RferlModulesPrefix) && w.GetTypes().Any(t => typeof(IValidator).IsAssignableFrom(t))); o.RegisterValidatorsFromAssemblies(assemblies); o.RunDefaultMvcValidationAfterFluentValidationExecutes = false; }) .SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddVersionedApiExplorer(o => { o.GroupNameFormat = "'v'VVV"; o.SubstituteApiVersionInUrl = true; o.AssumeDefaultVersionWhenUnspecified = true; o.SubstituteApiVersionInUrl = true; o.DefaultApiVersion = new ApiVersion(1, 0); }); services.AddApiVersioning(o => { o.ReportApiVersions = true; o.AssumeDefaultVersionWhenUnspecified = true; }); services.Configure <ApiBehaviorOptions>(behaviourOptions => { behaviourOptions.InvalidModelStateResponseFactory = (context) => { var errors = context.ModelState.Where(w => w.Value.Errors.Any()) .Select(s => new ValidationResult.ValidationError(s.Key, string.Join(",", s.Value.Errors.Select(e => e.ErrorMessage)))).ToList(); return(new ValidationFailedResult(errors)); }; }); var builder = new WebApiBuilder(services); return(builder); }