0

I've got an existing ASP.Net Web API with the default routing:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional });

I've got a controller - TestController and I want to have a number of GET methods within this controller e.g.:

public IHttpActionResult MethodA() -> http://....../api/Test/MethodA
public IHttpActionResult MethodB() -> http://....../api/Test/MethodB

Is there any way I could do something like this without modifying the existing routing and breaking the current controllers/routing?

2 Answers 2

1

Add Route attribute to your methods, like

[Route("MethodA")]
[Route("MethodB")]
Sign up to request clarification or add additional context in comments.

1 Comment

This worked but I also had to add a RoutePrefix attribute to the controller.
0

Can you modify

routeTemplate: "api/{controller}/{id}",

into

   routeTemplate: "api/{controller}/{action}/{id}",

1 Comment

I'd rather not as that would mean testing all the existing controllers. Adding Route and RoutePrefix attributes worked.

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.