3

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?

Subpar Web Dev
  • 3,210
  • 7
  • 21
  • 35
  • Your main obstacle will be overcoming the Same Origin Policy that most browsers enforce. You can start by reading up on that and solutions (Cors, JSONP, etc.) – Josh Noe Feb 12 '16 at 23:03
  • You should look at [`WebApi`](http://www.asp.net/web-api) _instead_. – EdSF Feb 12 '16 at 23:07
  • I fail to understand why you can't just call the method `yourdomain.com/controller/action` using WebClient or through javascript. – DPac Feb 12 '16 at 23:07
  • 1
    You don't need WebApi. You can do the same thing in MVC, but you will need to account for CORS. Have a look at this question http://stackoverflow.com/questions/6290053/setting-access-control-allow-origin-in-asp-net-mvc-simplest-possible-method – Brandon Feb 12 '16 at 23:19
  • (http://www.codeproject.com/Articles/866143/Learn-MVC-Project-in-days-Day) – Khan Abdulrehman Feb 13 '16 at 08:32

2 Answers2

3

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);
        }
    }
}
lastmannorth
  • 111
  • 5
2

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.

Brian Ogden
  • 18,439
  • 10
  • 97
  • 176