When to use a webservice, page Method? - asp.net

Just starting with JQuery. I have a couple of questions
When to use webmethods ,webservices , page methods , ashx
files in an ASP.NET 2.0 website ?
If I were designing an application where I'd use the webmethods on
multiple pages should I replace them
with a webservice ?
I suppose they all do the same thing but when to use which approach?
Thanks, Damien.

I don't use PageMethods anymore, but WebServices as it gives you a clean way to work with the data that you request. It also gives you the ability to use the data in other ways and in other places.
Have a look at http://api.jquery.com/jQuery.ajax/
To be more specific to your questions.
1. I might be bias, but since iv'e starting using asp.net mvc (I know you are using asp.net 2.0 WebForms), but it gives you a better feeling of separation of concerns and cleaner design.
PageMethods feels ugly for some reason right now. So, I'd personally advise against it and go with WebServices
If you are using a method in more than 1 page and it's generally to get data from an external provider such as a database or another webservice, it's best to extract this method into a webservice.
The approach I take is to think ahead just a little bit and don't over engineer. Keeps things simple and clean.

Related

CallBack functions, which is the proper way

So, I am fairly knowledgable in Asp.net 2.0 and Windows stuff mostly. A friend gave me his 3.5 book (Asp.net 3.5 SP1 in C# and VB Wrox). I know it is not the latest but it was free.
So in Chapter 2 they talk about doing Callback and not full post-backs. I use to accomplish this by an Ajax update panel.
They describe creating two JavaScript functions, the call wrapper and the result function and then the server side dynamically creates the caller using Page.RegisterClientScriptBlock. The class implements ICallBackEventHandler that calls RaiseCallBackEvent
My questions are:
what is the best method nowadays: the way described above, Ajax update panels, jQuery.Post, jQuery.ajax?
Under which Frameworks will each option work?
What are the situations where you might use one over the other? Like size or complexity of the parameters/return results?
Thank you
I would always use jQuery.post or jQuery.ajax over the use of UpdatePanel in ASP.NET WebForms.
It wins over performance as you are only requesting what you need, and there is no viewstate overhead.

Need example on building restful wcf web service with vb.net syntax

What I need is a good example of creating restful wcf with vb.net syntax, but all I can find is C# examples, and I spend my time trying to convert to vb.net. If anyone knows of good sample code in vb.net please let me know, or send me a link.
I would like to see how to create and consume collection data. anything would be a big help, I am struggling.
Tried having a look at this?
http://msdn.microsoft.com/en-us/library/bb412178(v=vs.90).aspx
or this:
http://www.vb-tips.com/Making_a_REST_service_with_VB_and_WCF.aspx
I know that there's somewhat limited resources in VB.NET out there, but look at the c# ones and get the idea of how yo achieve it and then implement the same thing in VB.NET, I promise it's not that hard, I've gone from VB.NET to C# myself once.

High level overview of ASP.net

I've spent a lot of time working in Django, and have grokked the framework well enough that I have started replacing the original components (view engine, etc.) with my own custom components and the sky hasn't fallen down.
I've been looking at ASP.NET MVC, and been quite interested (I really like C#/F#) but so far have learned... just about nothing. I've been digging through http://www.asp.net/mvc/mvc4 without much success. I suppose my main questions would be:
What are the main moving parts in a typical workflow? Let's say a request comes in. Who takes it, does stuff, and passes it on to who else? In Django, for example, a request goes through the URL Mapper, Middleware, goes to a controller, which may dig through some models (via explicit function calls) and get some data, pass it into a template (also via an explicit function call) to be rendered and pass it back.
What kind of client-server coupling is there? For example, in many frameworks there is a explicit coupling of each HTML-form with a serverside-validator, with a model, with a database table, such that client side validation code is automatically generated. Another example is Quora's Livenode, which explicitly links client-side HTML components with their dependencies in the model, allowing changes in the database to propagate and automagically update client-side code.
I think there is no better answer to your first question than ASP.NET MVC Pipeline :
http://www.simple-talk.com/content/file.ashx?file=6068
explained in more detail here :
http://www.simple-talk.com/dotnet/.net-framework/an-introduction-to-asp.net-mvc-extensibility/
To your second question : answer is none. ASP.NET application dont even have to render HTML output, you can write your own viewengine to give any representation of the data, not consumed by browser, but any http (REST) capable device. The only things you can consider as coupling "conventions" (for model binding for example), but they can be replaced and extended in any way you like.
What kind of client-server coupling is there?
As rouen said, none.
I am not familiar with Django, but unlike other MVC frameworks (including Rails) ASP.NET MVC is very skinny in that it only implements Views and Controllers of the traditional MVC pattern. You are pretty much on your own for the model part. That means there is no built-in support for database creation, ORM, et cetera.
ASP.NET MVC does implement a bunch of plumbing to route requests to the appropriate controllers and even some binding of parameters (e.g. query string parameters, form values) when instantiating controllers but this binding is not to a full blown model. The binding in this context is usually either single values or "viewModels"
ASP.NET MVC also implements the plumbing to select the right view to render.

VirtualPathUtility.ToAbsolute() VS. Url.Content()

I have been working in an MVC project, and have seen both of these used. I was wondering what the difference between them is? Is it incorrect to use one over the other? My understanding is that I should be using Url.Content(), but VirtualPathUtility.ToAbsolute() seems to be working as well.
Url.Content() is an utility method part of MVC. Being there to uniformize and centralize utility classes I guess.
VirtualPathUtility.ToAbsolute() is a .NET Framework method. Maybe the MVC one is using it, we would have to check the source...
Hope the helps
If you are doing this conversion within a Controller, then I'd favour VirtualParthUtility.ToAbsolute() over Url.Content().
The benefit comes when you want to unit test the controller actions. Rather than call it directly though, I'd define an interface IPathUtilities, say, with one implementation using VirtualPathUtility methods for the live site, and another using some sort of mock when testing.
If you call VirtualPathUtility directly, then you won't be able to test the action method (you might have thought some clever mocking of HttpContext would get round this, but having tried this myself I couldn't find a way to do it).

Architecture Critique: SQL Server FOR XML into XSLT for Rich HTML Interface

Can I get some constructive feedback about the following architecture?
Simplified Architecture Summary:
Return XML from your SQL Server (using FOR XML) and pass it straight into a XSL transform to produce a rich HTML web site.
What are the pro’s and con’s of such a system when compared with a conventional 3-tier ASP.NET architecture?
We have done something like this. And it works for very simple pages. But as soon as you would like to include some client side javascript and similar, you are doomed.
The generated output is hidden in the XSLT stylesheets and it is very hard to read, maintain and fix bugs.
Testing can be done, but also with much more effort than before.
The MVC pattern and similar is much better suited for such a scenario.
Two cons.
Data manipulation with C# or VB.net becomes harder because you don't have classes with properties (code intellisense) but xml-documents.
There are built in asp.net controls for data entry validation (both client side and server side). You can't use them if you use XSLT to produce your HTML-page.
I've done something similar in a project. I find the architecture very clean and scalable, but I would only advise you to use it if you happen to have lots of XSLT expertise in house.
We have a few XSLT templates, and a generic c# class that performs the transformation, using XSLT parameters. We get very good performance but, for new developers, the app can be hard to maintain.
One pro:
You can make XSLT-templates that produces HTML for the browser or XAML for WPF/Silverlight.

Resources