0

I want to create a route template for Owin WebApi like this:

cfg.Routes.MapHttpRoute(
            "API Default", "{myparam}/{controller}/{action}",
            new { id = RouteParameter.Optional });

Because I have controllers defined that need a parameter before the controller selection.

I have tried to remove the parameter and set it into RoutePrefixAttribute on controller but it doesn't work.

{controller} must be the first dynamic parameter of the route?

2 Answers 2

1

I would use some form of attribute based routing to go to different controllers based on {myparam}.

First controller:

[Route("param1/customer/{id}")] public IEnumerable<Order> GetOrdersByCustomer(int id) { ... }

Second controller:

[Route("param2/customer/{id}")] public IEnumerable<Order> GetOrdersByCustomer(int id) { ... }

More information can be found here: Attribute Based WebAPI Routing

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but {myparam} is not known at compile time, it's dynamic (like a category name, but it's not like this).
I would take a look here then, routing with optional first parameter
0

Delete RoutePrefix attribute and set the first parameter dynamic in your action Route attribute like the example below:

[HttpGet, Route("{myparam}/books/{bookId:int:min(1)}")]
public HttpResponseMessage Get(string myparam, int bookId)
{
...
}

1 Comment

You didn't answer question. How do you configure so it works.

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.