Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, July 22, 2011 2:06 AM
This deals with jQuery as well, but it's all about the same thing. For awhile, I used the following code to send data with jQuery to my controller via POST and retrieved a ContentResult:
var url2 = "/Instructor/UpdateSingleGradeOrResponse/4";
var data2 = "SubmissionID=" + subID + "&value=" + encodeURIComponent($(obj).val()) + "&type=" + type;
$.ajax({
type: "POST",
url: url2,
data: data2,
success: function (data) {
OnSaveSuccess(data, obj, type);
},
error: function () {
OnSaveError(request, status, error, obj);
}
});
In the controller "Instructor", I had an [HttpPost] public ContentResult called "UpdateSingleGradeOrResponse" that received the values that I specified above. Everything worked perfectly and occurred quickly. This ContentResult simply returns text.
I realized I hadn't used the [WebMethod] and didn't know if it was necessary. I always see it when it comes to asynchronous requests with jQuery/javascript, and wondered why my code still worked just fine. So, I added the [WebMethod] tag and reuploaded my project. I did whatever to trigger the ajax and everything still worked perfectly fine. This did cause a slight delay before the "success" of ajax.
I also decided to see if it would matter if [HttpPost] was still there, so I removed that. Therefore [WebMethod] was the only tag. This still worked the same as if I had [HttpPost] (slight delay still).
SO, my point is, what's the difference between all 3 of the methods I tried? Is there a benefit for using [WebMethod] over just [HttpPost]? It seems that [HttpPost] is (much) faster, so why not use it? This isn't some big, important project or anything, I just wanted to know reasons for which to use.
Let me know if you need more information. Any explanation would be awesome! (I don't just want links that tell me what [WebMethod] does, but I'd still be glad to receive them)
All replies (4)
Friday, July 22, 2011 3:18 AM âś…Answered
[WebMethod] declares that the method is a method of a web service. You don't need it; Tha ajax call just issue a normal post request without any kind of web service encoding, So your WebMethod attribute is simply ignored.
When you remove [HttpPost] everythig continue working because that attribute just require that only post will be accepted if you remove it than that action method accepts ant http verb...so your action method continue working. Normally you add HttpPost when you have two action methods with the same name, on for the POST and the other for the GET requests.
Tuesday, August 2, 2011 2:53 PM
Sorry I've left this sit for awhile now, but I still had a few questions.
First, thank you for the info!
If I don't need [WebMethod], then when would I?
I understood the meaning of using [HttpPost] and omitting it (POST vs GET).
Is there no difference in security when using [WebMethod]?
Tha ajax call just issue a normal post request without any kind of web service encoding
What kind of encoding is used with web services and how do you implement it?
I got this:
Remember, you can access Web services from many different types of applications, including other Web services, Web applications, Windows applications, and console applications. However, the client must be able to send messages to the Web service.
from the MSDN site. Again, how is this different from a normal [HttpPost] request?
Maybe I just don't understand what web services are...
Tuesday, August 2, 2011 4:30 PM
traditionally webservices meant SOAP (xml requests over http post). but ajax became popular and traditional webservices do not work well due to lack of xml parsing and construction support in the browser (newer browsers now support it). JSON over http quickly became popular for script services (web sevices designed to be called from javascript).
the simplest JSON parser in javascript is eval(json) and is built-in. the upload is often a form post because this is easy from script, but there are JSON libraries (and again newer browsers have built-in JSON serializsers).
in the meantime ms wanted it easier to write webservices (WCF, etc). so originally they added javascript support to simple webservices by supplying a url that would generate the javascript wrapper code. also instead of a standalone webservice (asmx), you could expose a global static method as a webserice vai the [WebMethod] attribute.
when JSON became popular more attributes where added for the websevice to support json results. the request has a content type, so the service can tell if its a form post/get, json post/get or soap post.
in MVC all global controller methods are web methods by definition (can be called via a url). in MVC [WebMethod] is just an attribute that can used as documentation or by a controller filter. You could write a filter that required some security token for example.
Tuesday, August 2, 2011 5:07 PM
When calling a webservice a protocol specific for encoding calls and returns values in a distributed environment is used. When using webservices in Internet this protocol is built on Http. Protocols are based on Xml or jSon.
Normally webservices that are called from javascript use json.
The action methods also can return and can receive jSon, however both the call and the return value doesn't use a complex encoding: everything is done by simply using the html protocol and setting the contenttype to text/json. The call is don with a simple get where the parameters are passed as query string parameters, or with a post. in most of the cases th parameters of the cal (both post or get) are not encoded as json. However you can do it, and you can also mix json with normal url and form encoding. The ModelBinder will decode them in all cases.
The drawback of using action methods instead of wcf webservices is that you cannot configure, its security, its headers, and son....simply because no webservices standard is used....but just simple json strings are exchanged by using the html protocol.