4

I want to write some API with single URL and want to route based on body type like below

    [HttpPost]
    [Route("{input: OverloadModelA}")]
    public IActionResult OverloadInsert([FromBody]OverloadModelA input)
    {
        return View();
    }

    [HttpPost]
    [Route("{input: OverloadModelB}")]
    public IActionResult OverloadInsert([FromBody]OverloadModelB input)
    {
        return View();
    }

My models like below

public class OverloadModelA
{
    public string AName {  get; set; }
}

public class OverloadModelB
{
    public string BName { get; set; }
}

As I know from https://stackoverflow.com/a/14358642/1539100, I need route based on type like "{id:int}" (overload action method).

but "{input: OverloadModelA}" is not working and I get this error:

InvalidOperationException: The constraint reference ' OverloadModelA' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'.

How can I overload action method based on custom type?

I am using .NET 8


I found some solution based on https://stackoverflow.com/a/1045616/1539100 that using ActionMethodSelectorAttribute but I think it is very hard and inefficient solution, so I hope some one provide more better (clean and good performance) solution for my question.

0

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.