Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core 3.1 API - 307 Temporary Redirect

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!

like image 664
NiAu Avatar asked May 20 '20 06:05

NiAu


People also ask

What is a 307 temporary redirect?

HTTP 307 Temporary Redirect redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location headers. The method and the body of the original request are reused to perform the redirected request.

What is the difference between 302 and 307 redirect?

302 is temporary redirect, which is generated by the server whereas 307 is internal redirect response generated by the browser.


2 Answers

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.

like image 144
Ali Kleit Avatar answered Nov 10 '22 12:11

Ali Kleit


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.

like image 21
Philipp Ott Avatar answered Nov 10 '22 11:11

Philipp Ott