AspNetCore.Proxy icon indicating copy to clipboard operation
AspNetCore.Proxy copied to clipboard

Question: Proxying for CORS in Development

Open c17r opened this issue 3 years ago • 1 comments

In prod, we're having HAProxy handle diverting any request beginning with /api/ to the backend server so everything is under the same origin and we don't have to worry about CORS.

For development we're using this library to handle the proxying in combination with spa.UseAngularCliServer(npmScript: "start"); Best I can figure, this is the simplest configuration to take anything request beginning with /api/, strip off /api/, and then proxying it to the backend:

app.UseProxies(proxies =>
{
	proxies.Map("api/{**rest}", 
		proxy => proxy.UseHttp((context, _) =>
		{
			var qs = context.Request.QueryString.Value;
			var url = context.Request.Path.ToString().Replace("/api/", "/");
			return $"https://localhost:5001{url}{qs}";
		}));
});

Is there another way built-in that I'm missing?

c17r avatar May 19 '21 18:05 c17r

This is the de facto best way to do this currently.

However, this does make me think that I could add a helper method to make it a little simpler.

twitchax avatar May 19 '21 22:05 twitchax