mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 14:20:29 +01:00
Page:
Cors
Pages
Admin API Reference
Compatibility WireMock.org
Conflict on Microsoft.CodeAnalysis.CSharp
Cors
Could not load file or assembly RestEase
Development Information
Faults
FluentAssertions
Home
KestrelServerOptions
Mapping
MimeKit and MimeKitLite
MyGet preview versions
Pact
Proxying
References
RegexExtended
Request Matcher FormUrlEncodedMatcher
Request Matchers
Request Matching CSharpCode
Request Matching GraphQLMatcher
Request Matching JsonMatcher
Request Matching JsonPartialMatcher
Request Matching JsonPartialWildcardMatcher
Request Matching JsonPathMatcher
Request Matching MimePartMatcher
Request Matching ProtoBuf
Request Matching Tips
Request Matching
Response Templating
Scenarios and States
Settings
Stubbing
Using HTTPS (SSL)
Using WireMock in UnitTests
Using WireMock.Net.Aspire
Using WireMock.Net.Testcontainers
Webhook
What Is WireMock.Net
WireMock as a (Azure) Web App
WireMock as a Windows Service
WireMock as a standalone process
WireMock as dotnet tool
WireMock commandline parameters
WireMock.Org
Xamarin Could not load file or assembly
Clone
Table of Contents
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Issue
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.
Solution
Cors support is not enabled by default, you can enable it when configuring WireMock.Net Server.
Option 1
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);
See also WireMock.Net.StandAlone.NETCoreApp/Program.cs.
📚 References
Pages
- 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