leora leora - 4 months ago 13x
JSON Question

Exposing a few calls from an existing asp.net-mvc site to other REST clients within an intranet?

I have an existing asp.net-mvc web site and now I need to expose of a few of my calls to external applications that are only used within my site right now. This is all happening within an intranet within my company.

I have read this page which explains Web API versus controller actions as well as this SOF question which seems to have a similar issue but the answers seem a bit outdated. So I am trying to determine given the latest available functionality what is the simplest solution to meet my requirement.

In my case, since I already have the same controller actions used within my current website then WEB API doesn't really make sense but if I google anything around asp.net-mvc authentication or security I only see articles around web API.

Given that, I am trying to figure out best practice for exposing my controller action to another application.

wal wal

In an ideal world you would convert the app to web api controllers as someone else suggested but to be more pragmatic you can implement an interim solution where you expose only the required calls via extending ApiController

You did not mention which version of MVC your current app is using nor did you mention how your current Controllers return data to the web app. I will therefore assume you return data via a view model and razor views. eg:

public class ProductsController : Controller
      public void Index()
            var view = new ProductsListView();
            view.Products = _repository.GetProducts();
            return View(view);

Suppose now you want to expose the products list via a REST-like api? First check you have web api installed (via nuget)

Install-Package Microsoft.AspNet.WebApi

(again i'm not sure what ver of asp.net you are on so this process may differ between versions)

Now in your public void Application_Start()

WebApiConfig.Register(GlobalConfiguration.Configuration);//add this before! line below
RouteConfig.RegisterRoutes(RouteTable.Routes);//this line shld already exist

and in WebApiConfig

public static class WebApiConfig
    public static void Register(HttpConfiguration config)
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }

I like to create a dedicated folder called ApiControllers and add controllers with the same name; this way you can have controllers with the same names as they are in different namespaces:

namespace YourApp.Web.ApiControllers
    public class ProductsController : ApiController
        public HttpResponseMessage Products()
              var result = new ProductResult();//you could also use the view class ProductsListView
              result.Products = _repository.GetProducts();
              return Request.CreateResponse(httpStatusCode, result);

You can then access this via yourapp.com/api/products

nb, try to reduce duplication of code inside controllers - this can be acheived by extracting common parts into service classes.