.NET Core 3.1 API - 307 Temporary Redirect
Asked Answered
A

2

12

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!

Attributive answered 20/5, 2020 at 6:47 Comment(0)
S
37

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.

Succession answered 17/1, 2021 at 18:2 Comment(1)
This was my problem with macOS, some people use IIS Express with Windows, so if you are using a local server be sure to comment out this line indeed.Venator
L
1

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.

Laryngo answered 17/6, 2020 at 19:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.