Arif YILMAZ
Arif YILMAZ

Reputation: 5866

How to convert a function into a WebApi function in MVC

I have an MVC project which has MVC App and WebApi in it. T

The WebApi is used by the MVC App to do the business logic and return objects. I had never tried to access to the Web Api with Chrom or somethingelse. But now, we have decided WebApi to be accessible by all our customer via HTTP protocol, I mean it should be accesible with Chrome like a regular WebApi projects.

What should I do to make the WebApi public to all? Should I add a TAG above of the function?

here are important parts of my ApiController which should be public

public class CustomerApiController : ApiController
{

[LocalizationWebApi]
[SessionControlWebApi]
[ScreenAccess(SCREEN_ACCESS.Show)]
public JsonResult<CustomerOrderFormSearchResult> CustomerOrderFormSearch(CustomerOrderFormSearchModel Model_)
{

    CustomerOrderFormSearchResult ret_ = new CustomerOrderFormSearchResult();
    ret_.Errors.Add(ResourceExtensions.Language("SHARED_MESSAGE_SESSIONCLOSED"));
        return Json(ret_, IgnoreEntityProxyContractResolver.Create());
}

}

Upvotes: 0

Views: 548

Answers (1)

MANISH KUMAR CHOUDHARY
MANISH KUMAR CHOUDHARY

Reputation: 3482

As I think you need to add the action verb with you controller action method example [HttpPost],[HttpGet],[HttpPut] etc. in your case you need to use [HttpGet] because it is plane get call. Modify your controller like

public class CustomerApiController : ApiController
{
[LocalizationWebApi]
[HttpGet]
[SessionControlWebApi]
[ScreenAccess(SCREEN_ACCESS.Show)]
public JsonResult<CustomerOrderFormSearchResult> CustomerOrderFormSearch(CustomerOrderFormSearchModel Model_)
{
       CustomerOrderFormSearchResult ret_ = new CustomerOrderFormSearchResult();
       ret_.Errors.Add(ResourceExtensions.Language("SHARED_MESSAGE_SESSIONCLOSED"));
       return OK(ret_);
       //Better to return Ok in your search result
      // If you want validation use NotFound() or something slimier to that 
 }
}

Always be careful about the route configuration of your api.

Upvotes: 2

Related Questions