-
-
Notifications
You must be signed in to change notification settings - Fork 209
Cors
Stef Heyenrath edited this page Jan 12, 2022
·
3 revisions
When calling WireMock.Net Server from a frontend applicatie (React / Angular), a CORS error is returned:
Cross-Origin Request Blocked:
The Same Origin Policy disallows reading the remote resource at http://localhost:9091/__admin/mappings.
(Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
Cors support is not enabled by default, you can enable it when configuring WireMock.Net Server.
var settings = new WireMockServerSettings
{
CorsPolicyOptions = CorsPolicyOptions.AllowAll
};
Note that these options are only available when running in .NET Core (3.1, 5.0 or higher)
## Option 2
Configure it manually:
``` c#
var settings = new WireMockServerSettings
{
// Other settings
};
/* Enable Cors */
var policyName = "MyPolicy";
settings.AdditionalServiceRegistration = services =>
{
services.AddCors(corsOptions =>
corsOptions.AddPolicy(policyName, // ◀️ MyPolicy
corsPolicyBuilder =>
{
corsPolicyBuilder
.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin();
}));
settings.Logger.Debug("Enable Cors");
};
/* Use Cors */
settings.PreWireMockMiddlewareInit = app =>
{
var appBuilder = (IApplicationBuilder)app;
appBuilder.UseCors(policyName); // ◀️ MyPolicy
settings.Logger.Debug("Use Cors");
};
// Start Server
var server = WireMockServer.Start(settings);
- Home
- What is WireMock.Net
- WireMock.Org
- References
- Settings
- Admin REST API
- Proxying
- Stubbing
- Webhook
- Request Matching
- Response Templating
- Unit Testing
- Using WireMock
- Advanced
- Errors