0

I have started building an ASP.NET Web API 2.0 application using the OWIN Middleware from Microsoft (http://owin.org/). I have several questions related to routing as I can't find a great tutorial on it for Web API. We have customers that could potentially have several companies (databases, essentially) that they want to service through this Web API. We wanted the user to be able to specify their company through a custom route. For example, a route like so api/MyCompany/Products will load all of the products for MyCompany. Obviously, we do not want to pass the company name into every action on every controller, so I want to create a custom route or custom routing middleware if standard routing won't work.

1) Are there any good tutorials on routing for Web API that I can look into to help me understand what I am doing and how I should be doing things?

2) Are there any good tutorials for building Middleware over OWIN for routing? Any in general?

3) Recommendations on a better way to do this, if there is one.

The companies are necessary because we don't want to hard code anything into a web.config file. Basically, the app will have info on all of the companies and, using the route, will get metadata such as the related connection string.

Any advice will be great! I know this probably isn't the usual post but I had no idea where to look. I haven't found anything of use via standard means (google and MSDN) unless I am just overlooking a simple concept.

1 Answer 1

1

Have you tried looking into Attribute Routing in ASP.NET Web API 2

which would allow you to set a route prefix on your controller to satisfy what you are looking for.

[RoutePrefix("api/{companyName}")]
public class CustomerController : ApiController {
    // GET api/MyCompany/products
    [Route("products")]
    public IEnumerable<Product> GetProducts(string companyName) { ... }

    // GET api/MyCompany/orders
    [Route("orders")]
    public IEnumerable<Order> GetOrders(string companyName) { ... }

    ....
}

if you read through the mentioned link you will see how to set it up and use it as needed.

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

1 Comment

Thanks! I will check it out.

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.