3

I am creating a RESTful webservice using ASP.NET MVC (not ASP.NET Web API). What I want to do is have every method in the controller return their result based on an input parameter (i.e. json or xml).

If I were using ASP.NET Web API, the HttpResponseMessage works for this purpose. When I attempt to return an HttpResponseMessage from a controller in ASP.NET MVC, there is no detail.

I have read that in this approach, I am supposed to use ActionResult. If I do this, then I need to create an XmlResult that inherits from ActionResult since it is not supported.

My question is why HttpResponseMessage does not work the same in both situations. I understand that in Web API, we inherit from ApiController and in ASP.NET MVC we inherit from System.Web.Mvc.Controller.

Any help is greatly appreciated.

Thanks,

EDIT 1 Much thanks to Fals for his input. My problem was in how to create an empty website and add all of the necessary functionality in. The solution was to use Nuget to get the packages mentioned in the comments and then to follow the steps in How to integrate asp.net mvc to Web Site Project.

Community
  • 1
  • 1
Lee Z
  • 802
  • 2
  • 13
  • 39

2 Answers2

2

Web Api is a Framework to develop Restfull services, based on HTTP. This framework was separeted into another assembly System.Web.Http, so you can host it everywhere, not only in IIS. Web API works directly with HTTP Request / Response, then every controller inherit from IHttpController.

MVC has It's implementation on System.Web.Mvc. coupled with the ASP.NET Framework, then you must use It inside an Web Application. Every MVC controller inherits from IController that makes an abstraction layer between you and the real HttpRequest.

You can still access the request using HttpContext.Response directly in your MVC controller, or as you said, inheriting a new ActionResult to do the job, for example:

public class NotFoundActionResult : ActionResult
{
 private string _viewName;
 public NotFoundActionResult()
 {

 }
 public NotFoundActionResult(string viewName)
 {
  _viewName = viewName;
 }
 public override void ExecuteResult(ControllerContext context)
 {
  context.HttpContext.Response.StatusCode = 404;
  context.HttpContext.Response.TrySkipIisCustomErrors = true;

  new ViewResult { ViewName = string.IsNullOrEmpty(_viewName) ? "Error" : _viewName}.ExecuteResult(context);
 }
}

This ActionResult has the meaning of respond thought HTTP Error.

Fals
  • 6,813
  • 4
  • 23
  • 43
  • 1
    Is it possible to use the Web API within a website? Both are MVC. Wouldn't this just be the packages that are included in each? – Lee Z Nov 20 '13 at 16:42
  • Yep, It is, the default template already comes with Web API. If you dont get It, just Install Web Api Web Host package from nuget! :) – Fals Nov 20 '13 at 16:44
  • If I wanted to start from an empty site, what would be the best steps to use? I used Nuget to get: Microsoft.Net.Http.2.2.18, Microsoft.AspNet.WebApi.Core.5.0.0, Microsoft.AspNet.WebApi.Client.5.0.0, Microsoft.AspNet.Razor.2.0.30506.0, AspNetMvc.4.0.20710.0, Microsoft.AspNet.Razor2.0.30506.0, and Microsoft.AspNet.WebPages.2.0.30506.0. Everything builds but it cannot display anything – Lee Z Nov 20 '13 at 18:15
  • The newest version is Web API 2, that comes with VS 2013 and VS 2012 by update :D: http://blogs.msdn.com/b/webdev/archive/2013/11/18/announcing-release-of-asp-net-and-web-tools-2013-1-for-visual-studio-2012.aspx – Fals Nov 20 '13 at 18:21
  • Got it to work - didn't have the right URL. Much thanks to you for your help! My big issue was that I could not figure out how to create it as a website. I had it working was Web Api project from template, but we do not want to release compiled assemblies when we deploy - hence the frantic rush. – Lee Z Nov 20 '13 at 18:33
0

As a matter of fact, it is indeed possible. You basically have two options:

  • develop your custom ActionResult types, which can be an heavy-lifting work and also quite hard to mantain.
  • add WebAPI support to your website.

I suggest you to do the latter, so you will have the best of two worlds. To do that, you should do the following:

  • Install the following Web API packages using NuGet: Microsoft.AspNet.WebApi.Core and Microsoft.AspNet.WebApi.WebHost.
  • Add one or more ApiControllers to your /Controllers/ folder.
  • Add a WebApiConfig.cs file to your /App_Config/ folder where you can define your Web API routing scheme and also register that class within Global.asax.cs (or Startup.cs) file.

The whole procedure is fully explained here: the various steps, together with their pros-cons and various alternatives you can take depending on your specific scenario, are documented in this post on my blog.

Darkseal
  • 9,205
  • 8
  • 78
  • 111