Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Angular js API with ASP.net webForms

I am using angular js with Asp.net webforms. I want to retrieve data from database using angular js API. All I have seen on google or any tutorial , uses angular js API's with Asp.net MVC .

My question is: Can we use angular js API with Asp.net web Forms?

If yes, then method will be the same as in Asp.net MVC?

Further I know angular js is for SPA(single page app) and it has Models views and controllers.

like image 646
Farman Ameer Avatar asked Oct 27 '14 06:10

Farman Ameer


People also ask

Can I use angular with a web form?

Hi you can use angular with web forms, and you can do single page application also by using $HTTP service in angular. i have written one ajax call by using web form. Thanks for contributing an answer to Stack Overflow!

How do I create a website using Angular JS in Visual Studio?

Create a new project and choose a blank ASP.NET Web site as the template in Visual Studio (any edition). Using Nuget Package manager, install Angular JS (mine was version 1.6.5). Don't worry, if you don't have Nuget. Just create a folder <Scripts> and unzip the Angular files available in the project.

How to add web API to a traditional Web Forms application?

Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.

Is there a simple CRUD application using Angular JS for webforms?

After hours of searching, I couldn't find a simple CRUD application using Angular JS for ASP.NET Webforms. This application gives an insight into developing Single Page applications using Angular JS in ASP.NET. The code attached herewith can simply be unzipped in a new folder and opened as a website.


2 Answers

Yes, you could.

But I really really don't recommend it. It might be extremely hard to ensure they are not stepping in each other toes, because they both work by enhancing the HTML.

My recommendation is that you start moving your business logic from the Web forms code behind into Service classes, so that you can expose the business logic via [WebMethods]. You could add the WebMethods to the pages but it would be way better if you could add a REST web service endpoint using WebAPI.

Think of an angular SPA as completely independent from the server technology, all you need for Angular to work with the server is an HTTP API, preferably one that returns JSON.

like image 162
guzart Avatar answered Sep 20 '22 17:09

guzart


Hi you can use angular with web forms, and you can do single page application also by using $HTTP service in angular. i have written one ajax call by using web form.

<script type="text/javascript" ><src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('MyController', function ($scope, $http, $window) {
        $scope.ButtonClick = function () {
            var post = $http({
                method: "POST",
                url: "Default.aspx/GetCurrentTime",
                dataType: 'json',
                data: { name: $scope.Name },
                headers: { "Content-Type": "application/json" }
            });

            post.success(function (data, status) {
                $window.alert(data.d);
            });

            post.error(function (data, status) {
                $window.alert(data.Message);
            });
        }
    });
</script>
<div ng-app="MyApp" ng-controller="MyController">
    Name:
    <input type="text" ng-model="Name" />
    <br />
    <br />
    <input type="button" value="Submit" ng-click="ButtonClick()" />
</div>
like image 34
Rajagopal Reddy ip Avatar answered Sep 19 '22 17:09

Rajagopal Reddy ip