Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Interesting Things about Web API in ASP.NET MVC

0.00/5 (No votes)
2 Nov 2019 1  
This post lists a few interesting things about Web API in ASP.NET MVC.

Here are a few things I learnt in WEB API which are a little interesting. I have spent lots of time fixing small issues and after some Googling, I found the solutions for those small issues, so I decided to document those solutions somewhere. The blog is a perfect place to document it so I can refer to it at any time.

Here is the list of issues and their solutions which I faced last night.

1) Not Able to Call WEB API from Client Application

When I started learning WEB API in ASP.NET MVC, I found most of the examples where the API has been called from the same project and it was working fine, but in the production environment, we have to call API from client applications. When I tried the same, it was not working.

Solution

You need to enable CORS in order to allow remote call to your web API.

  • Add System.Web.Http.Cors reference in your project. If you can't find it, add the nuget package for the same.
  • Go to the App_Start folder and click WebApiConfig.cs
  • Add these two lines in Register function:
    var cors = new EnableCorsAttribute("*","*","*");
    config.EnableCors(cors);  // add this line of code to allow cross domain request 

2) Custom API Name Was Not Working

If you have taken the default template for Web API development, it won't allow you to use your favorite name for the API, for example, if you want to use SubmitOrder for post API, it won't identify from client side call.

Solution

You need to add Route attribute to tell MVC that this is my API. Here is the example:

[Route("api/Test/SubmitOrder")]
public HttpResponseMessage SubmitOrder(FormDataCollection data)
{
    // your code goes here
} 

Before you add Route attribute to your function, you need to add the following line in your WebApiConfig.cs file.

config.MapHttpAttributeRoutes();

3) How to Return Data / Object in HttpResponseMessage

It is always a best practice to return HttpResponseMessage from your web API but how to return the data in the response? Most of the examples either return data / list of data or string or integer or HttpResponseMessage either.

Solution

You can include an object in your response like this:

[Route("api/Test/SubmitOrderThreePara")]
        [HttpPost]
        public HttpResponseMessage SubmitOrderThreePara(FormDataCollection frm)
        {           
            Customer cm = new Customer();
            cm.City = "Ahmedabad";
            cm.ContactName = "Test User";
            cm.Address = "Test Address";
           
            return Request.CreateResponse(HttpStatusCode.OK, cm);
        } 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here