2

How can I create the request mapping so that all requests GET, POST, PUT, ... and all paths execute the same method.

var application = WebApplication.Create();
application.Map("/", () =>
{
    return "This is all I do!";
});
application.Run("http://localhost:8080");

My guess is that there is a wildcard pattern that will work, but i have tried the following without success: "/", "*", "/*" and "/**".

0

2 Answers 2

3

See the Wildcard and catch all routes section of the docs:

app.Map("/{*rest}", (string rest) => rest);

Or you can use "catch all" middleware:

app.Use(async (HttpContext ctx, Func<Task> _) =>
{
    await ctx.Response.WriteAsync("Hello World!");
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can use application.Run with the RequestDelegate parameter instead of Map, to handle any and all requests:

public static void Main()
{
    var application = WebApplication.Create();
    
    application.Run(async context =>
    {
        await context.Response.WriteAsync("This is all I do!");
    });

    application.Run("http://localhost:8080");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.