skip to Main Content

I tried this and also this several links but not getting the answer.

this is my startup.cs file

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddCommonService(Configuration);
            services.AddSecurityServiceRepositories();
            services.AddSwaggerService();
        }
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseSwaggerService();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseStaticFiles();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            //app.UseMvc();
        }

  

Adding service class, it adds the repositories

    namespace Microsoft.Extensions.DependencyInjection
    {
    public static class SecurityServiceRepositoryCollectionExtension
    {
        public static IServiceCollection AddSecurityServiceRepositories(this 
    IServiceCollection 
         services)
        {
            services.AddTransient<IUserRepository, UserRepository>();
            return services;
        }
      }
    }
 

this is my swagger class file, it adds and uses the basic swagger service

    namespace Microsoft.Extensions.DependencyInjection
    {
    public static class SwaggerServiceExtension
    {
        public static IServiceCollection AddSwaggerService(this IServiceCollection services)
        {
            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title = "Sample API",
                    Version = "v1",
                    Description = "REST API for Sample "
                });
                options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = @"JWT Authorization header using the Bearer scheme. rnrn 
                      Enter 'Bearer' [space] and then your token in the text input below.
                      rnrnExample: 'Bearer 12345abcdef'",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });
                options.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name = "Bearer",
                            In = ParameterLocation.Header
                        },
                        new List<string>()
                    }
                });
            });
            return services;
        }
        public static IApplicationBuilder UseSwaggerService(this IApplicationBuilder app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Sample  Api V1");
            });

            return app;
        }
    }
}

   

This is my controller, I have tried the both attribute

    [Route("api/[controller]")]
    [ApiController]
    public class UserController : SecuredRepositoryController<IUserRepository>
    {
        public UserController(IUserRepository repository) : base(repository) { }

        [HttpPost("register-user")]
        // [Route("register-user")] I also tried this routing
        [AllowAnonymous]
        [ProducesResponseType(typeof(User), 200)]
        public async Task<IActionResult> AddNewUser([FromBody] User user)
        {
            try
            {
                var result = await this.Repository.RegisterUser(user);
                return Ok(result);
            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }
    }

This is coming on swagger UI instead of Controller, check the screenshot

2

Answers


  1. In my case I had to reference the project to the Host project which contains the Program

    Login or Signup to reply.
  2. I encountered this error when creating a new ASP.NET core web api project, but forgot to check "Use controllers (uncheck to use minimal APIs)" in Visual Studio. Recreating the project with that box checked solved the issue for me.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search