Can an API be built with ASP.NET web application?

Donald Symmons 2,861 Reputation points
2024-02-22T07:17:47.85+00:00

Hello Community, From the tutorials I have been seeing it looks like APIs are built only if an application is created with MVC. But is it possible to build an API with ASP.NET web application? And how do I go about from start? Thank you

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,649 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2024-02-22T07:59:22.9433333+00:00

    Hi @Donald Symmons,

    ASP.NET web applications have a dedicated ASP.NET Web API project that can be seen when you create the project from VS. Below is a tutorial for getting started.

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Albert Kallal 5,231 Reputation points
    2024-02-22T17:33:44.24+00:00

    You are free to build/create a specific web api project, and in near ALL cases, one is free to simple add web API (end points) to an existing WebForms site.

    You can create a 100% separate web method page (a asmx page), or you can even add web methods to existing web pages. So, say this web page and a web method (that is a API web end point).

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    
        <WebMethod>
        Public Shared Function MyHello() As String
            Return "Hello World"
        End Function
         
    
    

    So, the above web method? it will support a SOAP call (xml), a REST call, or better yet, a standard ajax call (and without any changes also supports JSON). So, in the markup, we can have this (we assume you have jQuery installed).

             
                <asp:Button ID="cmdTest" runat="server" Text="Web API test"
                    OnClientClick="endpointtest();return false;"
                    
                    />
                <script>
                    function endpointtest() {
                        $.ajax({
                            url: "TestAPI.aspx/MyHello",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            data: {},
                            success: function (data) {
                                alert("return value = " + data.d)
                            },
                            failure: function (rData) {
                                alert("error " + rData.d);
                            }
                        });
                    }
                </script>
    
    

    So there is little stopping you from adding web endpoints to a existing webforms site, and there is full support as such.

    Above when run results in this: User's image

    0 comments No comments