Asp.Net Core API published. Default page getting 500 error but Weatherforcast api working in Postman.

Jithesh k r 26 Reputation points
2022-02-11T05:05:45.03+00:00

Asp.Net Core Api published in IIS.
default page swagger/index.html not working and Login modules not working. but Weatherforcast api working in Postman. Please help.

Developer technologies | ASP.NET | ASP.NET Core
{count} votes

Answer accepted by question author
  1. Rijwan Ansari 766 Reputation points MVP
    2022-02-11T09:00:58.247+00:00

    Hi @Jithesh k r

    I think this is because of environment.

    // Configure the HTTP request pipeline.  
    if (app.Environment.IsDevelopment())  
    {  
        app.UseSwagger();  
        app.UseSwaggerUI();  
    }  
    

    You can keep swagger UI call outside of development environment.

    Try this way and check

    // Configure the HTTP request pipeline.  
    if (app.Environment.IsDevelopment())  
    {  
          
    }  
    app.UseSwagger();  
    app.UseSwaggerUI();  
    
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. Jithesh k r 26 Reputation points
    2022-02-11T10:02:22.237+00:00

    Hi @Rijwan Ansari ,

    My appsettings.json is

    {
    "Logging": {
    "LogLevel": {
    "Default": "Information",
    "Microsoft": "Warning",
    "Microsoft.Hosting.Lifetime": "Information"
    }
    },
    "AllowedHosts": "*",
    "ConnectionStrings": {
    "default": "Server=.;Database=AngularGYMDB;Integrated Security=true;"
    },
    "JWT": {
    "ValidAudience": "http://localhost:90",
    "ValidIssuer": "http://localhost:90",
    "Secret": "ByYM000OLlMQG6VVVp1OH7Xzyr7gHuw1qvUC5dcGt3SNM"
    }
    }

    and my startup.cs

    using CoreApiAuth.Authentication;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    using Microsoft.IdentityModel.Tokens;
    using Microsoft.OpenApi.Models;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace CoreApiAuth
    {
    public class Startup
    {
    public Startup(IConfiguration configuration)
    {
    Configuration = configuration;
    }

        public IConfiguration Configuration { get; }  
    
        // This method gets called by the runtime. Use this method to add services to the container.  
        public void ConfigureServices(IServiceCollection services)  
        {  
    
            services.AddControllers();  
            services.AddSwaggerGen(c =>  
            {  
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "CoreApiAuth", Version = "v1" });  
            });  
    
            // For Entity Framework    
            services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("default")));  
    
            // For Identity    
            services.AddIdentity<ApplicationUser, IdentityRole>()  
                .AddEntityFrameworkStores<ApplicationDbContext>()  
                .AddDefaultTokenProviders();  
    
            // Adding Authentication    
            services.AddAuthentication(options =>  
            {  
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;  
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;  
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;  
            })  
    
            // Adding Jwt Bearer    
            .AddJwtBearer(options =>  
            {  
                options.SaveToken = true;  
                options.RequireHttpsMetadata = false;  
                options.TokenValidationParameters = new TokenValidationParameters()  
                {  
                    ValidateIssuer = true,  
                    ValidateAudience = true,  
                    ValidAudience = Configuration["JWT:ValidAudience"],  
                    ValidIssuer = Configuration["JWT:ValidIssuer"],  
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JWT:Secret"]))  
                };  
            });  
        }  
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
        {  
    
            if (env.IsDevelopment())  
            {  
                app.UseDeveloperExceptionPage();  
                
            }   
              
            app.UseSwagger();  
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "CoreApiAuth v1"));  
    
            if (env.IsDevelopment())  
            {  
                app.UseDeveloperExceptionPage();  
            }  
    
            //app.UseHttpsRedirection();  
    
            app.UseRouting();  
    
            app.UseAuthentication();  
    
            app.UseAuthorization();  
    
            app.UseEndpoints(endpoints =>  
            {  
                endpoints.MapControllers();  
            });  
    
        }  
    }  
    

    }

    0 comments No comments

  2. Rijwan Ansari 766 Reputation points MVP
    2022-02-11T10:18:48.263+00:00

    In that case, you should check application logger. If you are using Azure App Service then you can use diagnostic. This is internal server error, you need to debug the solution

    173518-image.png

    0 comments No comments

  3. Jithesh k r 26 Reputation points
    2022-02-11T10:24:50.533+00:00

    Hi @Rijwan Ansari , the application is working through visual studio. but iis showing the 500 error. Where I can find the error log.

    0 comments No comments

  4. Rijwan Ansari 766 Reputation points MVP
    2022-02-11T10:29:18.827+00:00

    You can check IIS log for more details

    173522-image.png

    Additinally, ASP.NET exceptions in Window Event Viewer

    173450-image.png

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.