It really depends on what you are trying to achieve - aka how much abstraction you want to introduce.
If you wan't to move all the common business logic into a service for re-use from here, and potentially anywhere then yes you want to get rid of the System.Web.Http references.
Do this by having a clean interface/implimentation that simply return the result of the actions something like this:
public interface ICustomerService { BaseResponse DoSomething(BaseRequest request); } public abstract class BaseResponse { public bool IsSuccess { get; set; } public IList<string> Errors { get; set; } } /* Note: BaseResponse & BaseRequest, follow the command pattern for passing information you would impliment concrete versions of these. */
I then allow the controllers for both Web & Api control how to use this BaseResponse to er...respond.
So maybe create a BaseController, and BaseApiController:
For example:
public abstract class BaseApiController : ApiController { protected HttpResponseMessage HandleResponse(BaseResponse response) { return !response.IsSuccess ? Request.CreateErrorResponse(HttpStatusCode.BadRequest, response.Errors ) : Request.CreateResponse(HttpStatusCode.OK, response); } }
And:
public abstract class BaseController : Controller { protected ActionResult HandleResponse(BaseResponse response, string redirectToAction) { if (response.IsSuccess) return RedirectToAction(redirectToAction); foreach (var error in response.Errors) { ModelState.AddModelError(string.Empty, error); } return View(); } }
Then in WebApi Controller:
public HttpResponseMessage DoAction(string param1) { return HandleResponse(_customerService.DoSomething(new DoActionRequest { Param1 = param1 })); }
And in the Web Controller
public ActionResult DoAction(ViewModel viewModel) { var response = _customerService.DoSomething(new DoActionRequest { Param1 = param1 }); return HandleResponse(response, "Success"); }
In this way all busienss logic is tucked away and resusable, and the ApiController and Controllers can respond in their own unique ways.