Aless Hosry
Aless Hosry

Reputation: 1189

Webservice and website in the same project

I am reading a website project created on VS 2010 vb.net, where they have implemented a webservice.asmx in the same project to be called in javascript methods. In other words: the webservice is not implemented as API, it is a webservice.asmx where I can add methods to call database, and those methods are used in javascript functions.My questions are:

  1. What is the importance of calling a webservice from javascript instead of making a postback to the server and retrieve data form there?
  2. What about sessions? Does the webservice can view sessions of the user? I am asking this question because I can view some sessions filled as: HttpContext.Current.Session("UserID") = userId.
  3. If it is possible to catch and fill sessions of the user pages, is it possible to have access to controls in asp pages of the same user? And Why?

I am a little bit confused with this webservice, what I know is that webservice runs on a server and is used as API in applications... this is the first time that I work with a webservice and website written in the same project, thank you.

Upvotes: 0

Views: 618

Answers (1)

Markus
Markus

Reputation: 22456

1

Calling a WebService is a lot quicker than doing a PostBack, so if you want to only do partial updates of your website, a WebService is a good option to use (if you'd create the project from scratch, you'd use a Web API instead of an ASMX WebService). Of course, you have to integrate the result into the Web page on the client side. If you want to have asynchronous requests that lead to changes in the UI, you could also use an UpdatePanel on the ASPX-page.

It is also common to host the WebService in the same application as the Web frontend to avoid CORS issues.

2

The WebService can also access the session of the user if you set EnableSession on the WebMethod attribute to true. See this question for some pitfalls.

3

As the request to the WebService is a separate request it does not have direct access to a page's controls on the server (read in C#/VB.NET code), but you can change the HTML document tree on the client by using JavaScript.

If you need to share code on the server between the pages and the WebService, you should create separate methods in a helper/business logic class that are called by both the pages and the WebService.

To give an example, if both a page and the WebService need to get data from the database, you'd move the code for the database access from the ASPX page into a separate class (which is a good idea for many other reasons) and use the class both in the ASPX page and the WebService.

Upvotes: 2

Related Questions