[Solved] 307 Temporary Redirect by Kestrel server

The issue 307 Temporary Redirect was thrown by from the Kestrel server for me when I was trying to deploy the ASP.NET core application in Linux operating system. Due to this web application was not working.

Problem

When I was trying to deploy my ASP.NET Core web application I ran the command dotnet webUi.dll where WebUi.dll is my application file. I could see my application started running and it was listening on ports 5000 and 5001. The port 5000 was for HTTP calls and 5001 for HTTPS calls.

Running the application in Linux using dotnet command

Just to confirm whether I can browse the application which is running with the Kestrel server I tried to open the application with the CURL command as shown below, and I got a message saying HTTP/1.1 307 Temporary Redirect and that was from the Kestrel server.


307 Temporary Redirect message

If we observe the message, we could make out that HTTP calls are being redirected to https://localhost:5001. So all the HTTP calls are redirecting to HTTPS URL, due to this my web application was not working as expected as I was not ready for HTTPS deployment yet. I just wanted to test my application with HTTP first then I could set it up with HTTPS.

Solution

To prevent the automatic redirect from HTTP to HTTPS we have to go back to our source code and need to remove the automatic redirection. In my case, I could go to Program.cs file and remove the middleware code app.UseHttpsRedirection(). Once I removed the redirection code, I published the code for deployment again.


Middleware settings in Program.cs file of the application

After removing HTTPS redirect middleware, publish the application

Once deployed the new code, again I tried to run the application using dotnet WebUi.dll command where WebUi.dll is my application file. I got the same message as the appliction running on ports 5000 and 5001. The port 5000 for HTTP calls and the port 5001 for HTTPS calls.


Run dotnet command in Linux

This time when I ran the CURL command with http://localhost:5000 URL, I could see the HTML file of my application. That means my application running with the Kestrel server.


run CRUL localhost URL to check whether the application working with the Kestrel server

Video Tutorial

Summary

If the user wants to test the application after deployment with HTTP, the user can remove the automatic redirection middleware. Otherwise, the user has to set up the HTTPS with an SSL certificate (it can be fake or proper). In my case, I just wanted HTTP calls so I removed automatic redirection middleware from the code.

Sreenivasa Rangan

Software professional with 9+ years of experience in all phases of software development with robust problem-solving skills and proven experience in creating and designing software in a test-driven environment.

Post a Comment

Previous Post Next Post