.NET Core 3.1 API - 307 Temporary Redirect

17k Views Asked by At

When upgrading my .NET Core 2.2 API to .NET Core 3.1, a lot of changes were required. I had to update the Swashbuckle package and change the Startup file. I now got it running in dev with Swagger.

Once publishing to a Windows 2012 server, running on IIS, every API call returns the 307 Temporary Redirect.

In the Startup, I had to remove UseMvc and add UseEndpoints

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

if (!env.IsProduction())
{
    app.UseSwagger();
    app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Digital Signatures V1"); });
}

app.UseMiddleware<LogContextMiddleware>();

app.UseMiddleware<CustomExceptionMiddleware>();

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

Does anyone had an idea why I got this Redirect status?

When running it in Postman, it always returns the 307 when requesting the test server. When calling localhost, it is ok!

2

There are 2 best solutions below

0
On

If you are using HTTP, check if you have this line of code in Startup.cs and comment out:

// app.UseHttpsRedirection();

It is basically a middleware that redirects from HTTP to HTTPS. You may add it in production if you will use HTTPS.

0
On

I had a similar problem today (request getting 307 redirected) and it turned out to be https, and the SNI mapping in IIS.

Take a look if you can access your site with http://

It this works but not with https:// then there is your problem, most likely not with your code withhin, but some setting of IIS.