Home > Net >  How to host simple REST service using .NET core
How to host simple REST service using .NET core

Time:11-30

I'm trying to migrate WCF (WebHttpBinding) to .NET core. Because, WebHttpBinding is not available in coreWCF.

Here my server code to host the REST service:

var builder = WebApplication.CreateBuilder();
var app = builder.Build();
app.Urls.Add("http://localhost:9000/MyService");
app.MapGet("/Test", () => "Hello World!");
app.Run();

Here my client code to call the service:

using HttpClient client = new();
client.DefaultRequestHeaders.Accept.Clear();
var reply = await client.GetStringAsync("http://localhost:9000/MyService/Test");

The client code works good if the service is developed using WCF (WebHttpBinding). However, above server-code does not work.

How should I create REST service, so that the client-code can access?

CodePudding user response:

Can you try creating the service like this?

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.Urls.Add("http://localhost:9000");
app.MapGet("/MyService/Test", () => "Hello World!");

app.Run();

The URL should not contains paths.

  • Related