Forum Discussion

ChristopherKiraly's avatar
ChristopherKiraly
Copper Contributor
Mar 07, 2024

Comparing 2 startup.cs files from different .NET Core APIs

tl;dr -- my .NET 5 Web API is working on localhost and not deployed - but when I compared program and startup to an API that is working, there are lots of differences I don't understand.

 

Using .NET 5 (yes I know its out of support - but its what I have to use right now based on server, etc).

 

So here is my issue...  I developed an API, that when I run through IIS on my local machine, works as expected (https://localhost:57995/mailer/pdfmaker - the get returns the expected value and the post does the programmed workload and again returns the expected result json).

I've tried to deploy this on Server 2019, where a website is running, and has several different APIs attached (.NET 5, as well as a few .NET Framework 4.5 / 4.72 APIs).  When I deployed the API, I've tried the following ways:  

Deployment Mode as Framework-Dependent AND Self-Contained, with Target Runtime being Portable and win-x64.

 

Both ways generate a 404 error when I try to go to https://website_url/mailer/pdfmaker.  (Via browser and Postman for API testing).  This is what I'm trying to fix.

 

In reviewing what has been used before, I'm trying to determine what I'm missing.  When I created the 'solution', I used the default .Net 5 Web API method, so the program.cs and startup.cs are simply what are the default creations by Visual Studio.  However, I noticed that the other .NET 5 API (that is working) is using the following program.cs and startup.cs...  and I'm hoping someone can tell me what these are doing and why, so I can figure out what is different...

 

Here is the 'working' program.cs:

 

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Classic.Production.Tracker {
    public class Program {
        public static void Main(string[] args) {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                   .UseStartup<Startup>();
    }
}

 

 

here is the default program.cs (that works on localhost, but not deployed on the webserver):

 

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;    //unused
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;          //unused
using System;                                //unused
using System.Collections.Generic;            //unused
using System.Linq;                           //unused
using System.Threading.Tasks;                //unused

namespace classic.api.pdfmaker {
    public class Program {
        public static void Main(string[] args) {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder => {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

 

 

 

As for startup.cs, here is the 'working' startup.cs:

 

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace Classic.Production.Tracker {
    public class Startup {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services) {
            services.Configure<CookiePolicyOptions>(options => {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddMvc(option => option.EnableEndpointRouting = false);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
            // Always enable the error page (for now).
            app.UseDeveloperExceptionPage();

            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes => {
                routes.MapRoute(
                    name: "areaRoute",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
                );

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}"
                );

                routes.MapRoute(
                    name: "api",
                    template: "Api/{controller}/{action}/{id?}"
                );
            });
        }
    }
}

 

 

and here is the default startup.cs (which is only working on localhost - not deployed):

 

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace classic.api.pdfmaker {
    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();
        }

        // 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.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints => {
                endpoints.MapControllers();
            });
        }
    }
}

 

 

So like I said, I'm hoping someone can explain to me what the differences are, and maybe even why the non-stock version is working when deployed, but the stock version of the files are not!

No RepliesBe the first to reply

Resources