Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET
Technical Blog

Interesting Things about Web API in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.41/5 (5 votes)
2 Nov 2019CPOL2 min read 3.1K   3   2
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:
    C#
    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:

C#
[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.

C#
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:

C#
[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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Netclues Technologies India Pvt. Ltd.
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseExpecting more tips and tricks on web API's Pin
Zindam868-Nov-19 19:42
professionalZindam868-Nov-19 19:42 
QuestionInclude object in response Pin
hegn4-Nov-19 20:37
hegn4-Nov-19 20:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.