The goal of this project is to provide help when client app can't access some public rest web api because of cors restriction. It appears, at least in some cases, that such restriction doesn't apply when running server project, so this proxy has a place to jump in as a middle man.
The plan is to keep this simple as possible, so it will be extended just as some specific requirement may ask.
Initial run provides proxy on get requests for given url, no error handling and no fancy at all.
Here is how the goal has been met.
dotnet new webapi -o DotnetProxy
cd DotnetProxy
dotnet new gitignore
Initial template gives us Weather forecast something... no need for that now - delete wf class file, rename controller and it's class to ProxyController, remove references to wf in controller.
New class ProxyService. To get result from external source. One using declaration is needed:
using System.Net.Http;... because we'll deal with Http messages, request and response. Here:
public string Get(string url)
{
using (HttpClient client = new HttpClient())
{
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
HttpResponseMessage response = client.SendAsync(request).Result;
return response.Content.ReadAsStringAsync().Result;
}
}Register service for dependency injection in ConfigureServices method of Startup:
services.AddScoped<ProxyService>();Now service can be used in ProxyController, to declare it:
private readonly ProxyService _service;
//...
public ProxyController(
//...
ProxyService service
)
{
//...
_service = service;
}... and then use:
[HttpGet]
public object Get(string url)
{
return Content(_service.Get(url), "application/json");
}This project was introduced to help with missing cors response headers, it would be nice that it provides some to be better for that at least.
In Startup declare...
private readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";Again at method ConfigureServices, add some cors lines:
services.AddCors(options =>
{
options.AddPolicy(
name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://localhost:3000");
});
});Yes, that is because I intend to use my client apps from this address, otherwise it can be adapted to different needs.
And, for the end, to add use of cors to Configure method pipeline:
app.UseCors(MyAllowSpecificOrigins);Important notice here that it should be placed after UseRouting call and before UseAuthorization - that is pipeline of processing request from one middleware layer to another, some of them are maybe somehow dependent so specific order sequence should be followed (I haven't studied this whole process in detail yet, but I'm wise enough to accept fair recommendation).
And - that is all - ready to use!
... if this tends to grow, it will get it's snapshot branches, no worry...