0

I have a controller action which return a JSON data. It can be easily accessible through a getJSON method. But I want that JSON data to be retrieved through web API.

My controller code is

public ActionResult GetProfile(string profileName)
    {
        var profileDataService = new BokingEngine.MasterDataService.GetProfileDataService();
        var request = new ProfileSearchCriteria { Name = profileName };
        var profileDetails = profileDataService.GetList(request);
        return Json(profileDetails, JsonRequestBehavior.AllowGet);

    }
2
  • Check this link and Link 2 Commented Mar 13, 2013 at 6:05
  • BTW your code is using ASP.NET MVC, not ASP.NET Web API. Are you trying to convert the code from MVC to Web API or something else? Commented Mar 13, 2013 at 6:14

2 Answers 2

5

In Web API, this would be your action:

public class ProfileController : ApiController {
    public ProfileData Get(string profileName)
    {
        var profileDataService = new BokingEngine.MasterDataService.GetProfileDataService();
        var request = new ProfileSearchCriteria { Name = profileName };
        var profileDetails = profileDataService.GetList(request);
        return profileDetails;
    }
}

Then your client should specify the data type they want. If an Accept: application/json header is specified, Web API returns JSON. Accept: text/xml will yield XML.

More info: http://www.asp.net/web-api/overview/formats-and-model-binding/media-formatters

Sign up to request clarification or add additional context in comments.

1 Comment

It wont work in my case as 'profileDetails' and 'ProfileData' is a model class. I cant return list to class
0

Change your ActionResult to JsonResult so it returns Json. If you want to make Web API specific, you should create a Controller that inherits ApiController.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.