Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I got a 404 on a double slash after upgrading to inProcess

i have .net core 2.2 web api i upgraded it to "inProcess", client's who call this service with double forward slah got 404, when configure it to "OutOfProcess" it's work just fine. example of the problematic call:

https://mMYApiAddress.co/api/GetSomeData//3

when configure it to "OutOfProcess" it's work just fine.

i want my web api to work "inProcess" configuration and will be able to get calls with double slash as before.

like image 770
Omri D Avatar asked Mar 04 '23 08:03

Omri D


1 Answers

When configuring "OutOfProcess" , Kestrel web server is used to process your requests. In Kestrel web server, spaces or extra strings in the request path are automatically recognized and the path is modified to the correct format.

When configuring "InProcess" ,IIS HTTP Server (IISHttpServer) is used instead of Kestrel server.Requests are processed directly by the application. So you got the 404 response .

You could customize a middleware to rewrite the url like below :

public class RewriteRouteRule
{

    public static void ReWriteRequests(RewriteContext context)
    {
        var request = context.HttpContext.Request;
        if (request.Path.Value.Contains("values"))
        {
            string[] splitlist = request.Path.Value.Split("/");
            var newarray = splitlist.Where(s => !string.IsNullOrEmpty(s)).ToArray();
            var newpath = "";

            foreach (var item in newarray)
            {
                newpath += "/" + item;
            }
            request.Path = newpath;
        }
    }
}

Use the middleware in Configure method of Startup.cs

app.UseRewriter(new RewriteOptions()
                      .Add(RewriteRouteRule.ReWriteRequests)
                      );
like image 60
Xueli Chen Avatar answered Mar 21 '23 08:03

Xueli Chen