Placing service classes in subfolders

Topics: Web Api
Oct 11, 2011 at 2:34 AM
Edited Oct 11, 2011 at 12:04 PM

Hi, folks. Up until now I have been placing all my RESTful service classes and their DTOs in the root of my Web project. Here's an example structure:

Services project
-- Product.cs
-- ProductsService.cs

I'm configuring the routes like so:

routes.MapServiceRoute<ProductsService>("products", configuration);

Everything works perfectly with that approach. However, my project is starting to get a bit messy due to all the files in the Services namespace. In order to get a bit better organization, I tried to place my service class and its DTOs in a subfolder within the Web project. The structure looks something like this:

Services project
-- Products
-- Products/Product.cs
-- Products/ProductsService.cs

When I try to access my service via its standard URL http://localhost:1234/products, I get the following response:

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/products/'>here</a>.</h2>
</body></html>

This is followed up with a 404 error response. I am wondering how the route should change to accomodate the existence of the Products subfolder. Can anyone help?

Oct 11, 2011 at 6:13 AM

To my understanding moving code files to different folders and namespaces does to affect the service location when accessing the service from web. You might want to check you MapServiceRoute calls for refactoring errors. You also didn't specify did you move the ProductsService.cs also as MapServiceRoute is using it instead of ProductService.cs which you said you moved to new folder (if you are using filename which match your class names).

Oct 11, 2011 at 12:12 PM

I spotted a typo in my original post. ProductService.cs should be ProductsService.cs (in my actual code it is correctly named).

I assure you that MapServiceRoute is coded correctly. There's not much with that method call that one can get wrong.

Somehow, the existence of a folder that matches the route causes problems. I should note that renaming the folder to Products2 allowed the code to work once again. The problem only occurs when the route matches the folder.

Oct 11, 2011 at 12:37 PM

A quick update: I configured the ASP.NET Route Debugger and noticed that when there is a folder name matching the URL structure, the route debugger doesn't even execute. This leads me to believe that the ASP.NET Web Development Server itself is actually handling the request completely and that the request never makes it to the ASP.NET routing engine.

Oct 11, 2011 at 12:46 PM

After reading this post I am inclined to believe that this is working as intended due to the way IIS and ASP.NET integrate. I think it makes sense that IIS should handle several types of requests (folders, content files, etc.) while leaving ASP.NET-specific requests to ASP.NET. Unfortunately Visual Studio projects don't have any kind of "virtual folder" capabilities that other IDEs like Xcode have, so I'm stuck having to place these service folders within App_Code. The extra level of folderness won't hurt too bad. :) I also set the Namespace Provider setting on the App_Code folder to False to preserve the namespace structure I was looking for.