Subpar Web Dev
Subpar Web Dev

Reputation: 3260

How do I make an ASP.NET MVC controller that can take requests from an external site?

I want a controller like

public ActionResult UnlockAssets ( string psd ) 
{
    // ... 
}

that external sites can call and have JSON returned from. Is this possible? If so, where do I start?

Upvotes: 3

Views: 1310

Answers (2)

Brian Ogden
Brian Ogden

Reputation: 19212

Yes it is possible the external site would call your controller via Ajax for example.

The external site would have a Javascript function with Ajax that would call the url:

http://www.yourwebsite.com/yourcontrollername/UnlockAssets?psd=value

Your response can be JSON, you could use the ActionResult signature and return JSON but formatting might be off and the header response header might be incorrect, by NOT saying you have a JSON response to the requester/client.

So this would be the correct ASP.NET MVC signature method for your controller

[HttpGet] //or [HttpPost] or both
public JsonResult UnlockAssets ( string psd ) 
{
    return Json(new { foo = "blah"}); 
    //or
    return Json(someinstanceofaclassofyours); 
}

You do not need Web API as someone suggested, you might prefer Web API but really it just depends on what is right for you, your timelines, what your clients need when they call your server etc, the complexity of your architecture, the complexity of the clients' needs.

A normal ASP.NET MVC Application Controller can act as an "API" in simple architecture (even a complex) one and serve the needs of external clients just fine. That controller can server Views for your ASP.NET MVC site as well as return JSON to an external caller. If that Controller seems like the right way to organize your architecture than fine.

When making architectural decisions you may consider Web API more appropriate when you are building an actual API for clients to consume, this API would have documentation, expose various methods for client callers, be robust. Web API can also be used for simple method for clients to call. There is no right or wrong choice it is more just a the right tool for the job.

I would say, if you have Models, Entity Framework interwoven in an ASP.NET MVC Application already, and you just have a method you are trying to expose to an external client, you don't see your MVC Application growing quickly and getting out of hand, just use a controller. Otherwise use Web API either by adding Web API to your existing MVC project or preferably adding a new Web API project to your solution.

Upvotes: 2

lastmannorth
lastmannorth

Reputation: 181

You probably want a Web API (http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)

Heres a simple example

using System;
using System.Linq;
using System.Net;
using System.Web.Http;


 public class DocumentsController : ApiController
{
    public IHttpActionResult UnlockAssets (string psd )
    {
        var documents = new DocumentRepo();
        if (!documents.Exists(psd))
        {
            return NotFound();
        }else{
            documents.unlock(psd)
            return Ok(product);
        }
    }
}

Upvotes: 3

Related Questions