Combine a web app and a web service in the same project

How to create/configure an ASP.NET project that combines a web app and a web service.

.

Introduction

Web app = web user interface (UI), intended for browser users

Web service = programmatic interface

.

Problem – we want to use the same ‘controller’ names in the URI

For example…

http://example.com/sports

http://example.com/api/sports

Implication – controllers will have the same class name, “SportsController”.

Problem – this will not work if they are in the same namespace.

Solution – use different namespace names.

.

Source code organization alternatives

Organizing your source code:

  1. Use one source code file to hold the web UI controller and the web service controller
  2. Use separate source code files

.

Use one source code file to hold the web UI controller and the web service controller

Probably not a good idea.

Must reorganize both classes, by moving the ‘using’ declarations inside the namespace block. Also, the size of the source code file will grow, perhaps to an uncomfortable size.

Instead, use the other alternative…

.

Use separate source code files

This is probably a better idea.

Use a prefix or suffix for the source code file name. For example:

Web app: SportsController.cs

Web service: Any of these:

  • APISportsController.cs (or apiSportsController)
  • SvcSportsController.cs (or svcSportsController)
  • WSportsController.cs (or wSportsController)
  • SportsControllerAPI.cs (this is the one that your professor will use)
  • SportsControllerService.cs
  • SportsControllerWebService.cs

The bottom line is be consistent, whatever scheme you choose.

.

Namespaces

If the project’s primary function is to be a web app, then the web UI controllers will be in a namespace named “<project>.Controllers”.

In that situation, edit the namespace of the web service controllers to be in a namespace named “<project>.Controllers.API”.

Alternatively, if the project’s primary function is to be a web service, then the web service controllers will be in a namespace named “<project>.Controllers”.

In that situation, edit the namespace of the web UI controllers to be in a namespace named “<project>.Controllers.UI”. Alternatively implement the suggestion above.

Finally, if the project must be equally a web app and a web service, then just choose one of the suggestions above and stick with it.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: