Error 405 – Methods not allowed in Asp.Net Core application

I developed an Api in Asp.Net Core Web Api. While testing it on my local machine verything works fine. When I deployed it to my Ionos Managed Windows Hosting I got a 405 Http Error on some Endpoints. Analyzing this behaviour showed me that only controller methods with PUT and DELETE annotations throwed the error.

The problem seems to be the WebDav Module of IIS blocking PUT and DELETE requests by default.

Solution: Remove WebDav in the web.config file.


<system.webServer>
<handlers>
<remove name="WebDAV" />
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<modules>
<remove name="WebDAVModule" />
</modules>
</system.webServer>

Using Refit with Factory and Proxy

Sometimes a webhost wants you to use a proxy to communicate via http with an API for example. If you use Refit in C# with Factory implementation you can use the following code:


services.AddRefitClient()
.ConfigureHttpClient(c => c.BaseAddress = new Uri(Configuration.GetValue("The name of the value in your appsettings.json")))
.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler()
{
UseProxy = true,
Proxy = new WebProxy("http://yourProxyUrl:yourProxyPort", false),
PreAuthenticate = true,
UseDefaultCredentials = false
});

Set multiple startup projects in Visual Studio

There are use-cases where you want to start multiple projects of your solution at once in Visual Studio. For example if you’re developing a website in Asp.Net Core MVC getting it’s data of an Asp.Net Core Web API application. You’ll have to do the following steps to set multiple startup projects:

1) Right click on the top node of your solution in the solution explorer.
2) In the context menu choose Properties (Alt + Enter).
3) Expand the „Common Properties“ node, and choose Startup Project.
4) Select the Multiple Startup Projects radio button option and set the appropriate actions.

The way with less clicks is to click on the little arrow on the left side of the start button and choose Define Start Projects or right click on the solution node and choose Define Start Projects.

Automapper returns empty Collection after mapping

Some days ago I had a problem while mapping domain objects to DTO by automapper. My environment was an Asp.Net Core 3 Web Api project. I created an own profiles class where I stored my mapping logic. In the startup class I registered the mapper service. When running project my source of type List<Product> should be mapped to List<ProductDTO>. But the API always returned an empty list even if the source was filled correctly.

Profile:


CreateMap<Product, ProductDTO>();

CreateMap<List<Product>, List<ProductDTO>>();

The mistake was the second line in my profile class. Automapper automatically maps collections you don’t have to create a map for this case.