plaese guide me to start asp.net mvc 4 ajax enabled multi platform web application. I have a REST service hostrd in another domain, want to consume it. please guide me whether i need to access that service from javascript ajax call or
create models at server and request my server with ajax call. any guides links much appreaciated..
regards
ani
The exact way to consume your web service will depend on the content of this service, and how you want the user to interact, but, from my point of view, the server should be interacting with the webservice, and your user with your server. That is, as you guessed, "models" should reflect your web service data structure.
If you want to use ajax, use it to call methods in your controller(s) to load/update data from the webservice.
It's a bit difficult for me to go further without knowing what we are talking about, but to give you a better idea of what I am talking about, I wrote my own "API" to deal with Amazon Products Advertising API, using their rest service instead of SOAP. This same API is being used by 3 differents web applications.
Inside each web application there are AJAX calls to controller methods, who in turn calls my API methods.
This mechanism works perfectly in this case, but it may not in yours, which is why I believe you need to tell us a little more about hte kind of service and application you are going to build.
Sorry to stay a little vague, still hope this helps.
Bernard
Related
I am planning to create a service oriented application. For creating service I will be using WebApi and for web application MVC.
I have thought of two approach for the same kindly let me know which one better.
Create one business logic shared across MVC and WebApi and dont consume WebApi in MVC application. Just business logic will be shared.
Consume API in MVC application from WebApi project using HttpClient.
The question was a bit unclear but now, after comments it makes more sense.
I would go with option 1.
The first reason is that you can avoid a lot of traffic to the API if you can simply use the layer and don't bother with any APi calls at all.
The second reason, it will be a little quicker since it doesn't need to wait for the API responses.
Less pressure on the API means the mobile app has more resources to play with.
You can basically build your own Nuget packages, use them in both your Web and Api layers and you don't have to repeat the code.
or must it be on the same server as the app calling it? I am new to web api so i am going through some tutorials, but they all assume the web api is part of the mvc app. Also, they show the calls to the api being done with javascript, but I want to make the calls in my MVC app controller. Is this possible?
You can host a Web API anywhere.
The only special thing to have into account when the Web API isn't in the same server that a web site that uses it, is that, by default, the Web API doesn't accept requests from a different domain. For example, if the web site is in "server1.com" and the Web API in "server2.com", then the calls to the Web API from the web server will be rejected.
If this is the case, you need to configure the Web API server to enable CORS (cross origin resource sharing), so that it accepts requests from a different domain. If you want more info about this, please look at this document:
Enabling Cross-Origin Requests in ASP.NET Web API 2
The Web Api can live wherever you want it to. Is typical to see a limited API used mostly to handle AJAX for the MVC application live with the MVC application, mostly because it makes it simpler to construct URLs to the endpoints. If you host the Web Api externally, then you'll have to hardcode the API endpoint URLs, as there's no way to use something like Url.Action to generate them automatically, any more. Regardless, it's a perfectly acceptable way to handle things.
You will probably at least want to add the base URL for the Web Api as an app setting in your Web.config, though. That way, you don't end up with hardcoded references to a particular domain strewn all about your app. That makes moving your Web Api to a different domain much easier, especially when talking about going from development to production.
It is also entirely possible to use a Web Api within your actual controller actions. You'll just need to use something like HttpClient to connect to it and issue requests.
I am developing a single page app running in desktop browser, tablet browser and maybe phone browser. I am return only JSON from my backend wether it is ASP.NET MVC or Web API.
When I think of Web API or read about it I always hear the words REST/RESTfull. Independently from what REST is I like some features about Web API which I have not in MVC (by default but maybe it can be implemented somehow but I dont want that extra effort...)
Return HttpStatus codes like 200 for GET or 201 when the Ressource is created. My single page app knows and reacts on this codes.
Return DTOs directly in the controller and c# classes are automatically serialized to json. With MVC this is not a one-liner. Not dealing with ActionResults.
Web API is very much designed about 'cool' urls/routing I will also have deep/complex routing on my client.
My Web API endpoints are just 'ajax callbacks' I do not need REST things like include a self.link in every retrieved ressource etc...
Of course I do not create/modify ressources on a Get request. But I already have been used to this style when I was doing MVC. So I like and will do many REST styles but not because of REST itself rather its common practice.
Should I really design great restfull API`s to use the Web API ? I do not know what is Microsofts recommendation about the Web API or wether they have really a guideline about it...
In my experience Web API has often been a great help, and rarely a hindrance. If you do it "their way," it saves you a lot of ugly work around serialization/routing/binding. If you don't want to do it "their way," you don't have to. You can extend the routing engine. You can implement your own serialization. You can return raw JSON where you see fit. Nothing in Web API forces you to follow the REST concept to a T (trust me I have seen this in action). I haven't heard this complaint before from anyone who has used it - I would suggest you give it a try and see just how far off it really is.
REST is not at all required to use Web API. In fact, it is quite easy to turn on and use session management as well.
In fact, while RESTful approaches have some real advantages, I wouldn't recommend going 100% RESTful. It is simply not possible to make them fully secure without using a third-party authentication provider. We built a fully RESTful prototype and explored many different mechanisms to secure the site. In the end, though, every one had one vulnerability or another (it helps to have a CISSP on staff). So I talked to one of the top security experts at Norton and he agreed that, yes, there is always a way to exploit a fully RESTful, standalone app. Symantec is apparently building a "wrapper" technology that gets around this but it was easier to either go with a third-party authentication provider or just go back to MS-based security using session cookies.
I am quite familiar with ASP.NET MVC and know that a controller’s method can respond with XML and JSON apart from other things (i.e. behave like a restful webservice). I am just wondering whether I can make a controller method behave like a soap web service which responds with a SOAP XML response to a POST request? Maybe it is just question of responding with XML but this would be more like a restful web service. I want to avoid having to implement a separate web service project if possible. Any feedback would be very much appreciated. Thanks.
Christian
You don't need to implement a separate web service project. All you need is to add a WCF service endpoint (.svc file) to your current web project. Also you are saying that you expect SOAP response after a GET request which of course doesn't make much sense because in the SOAP specification requests should be POST.
I believe your thought is to create a single deployable MVC Web Application that can respond to both SOAP requests and RESTful requests (maybe even more?). I have thought about this myself, however there is no point in re-inventing WCF as it can do both without any additional programming. The ASP.Net MVC assemblies were not designed to function as a web service, and although it can, probably shouldn't be when other technologies exist that were designed specifically for that purpose.
A 3rd party site sends its notifications after my web application has completed some action in order to notify me of its success. Receiving a notification item requires a response back to the 3rd party server (URL) with the a containing the value "accepted".
I have never user SOAP and with the basic info found I'm a bit lost for the case of asp.net mvc. Are there any good links showing the principle of receiving and sending SOAP responses?
Tutorials / information may be presented in other languages such as java, asp.net (classic) or something. I need to get a general idea since googling on SOAP has not given me anything for the past few hours.
You need to learn a little about WCF. See the WCF Developer Center, especially the Beginners Guide.
What you want is to create a simple WCF service that corresponds to the WSDL that they will give you. You will need to implement only the operation (method) that they will call to notify you. You can host a WCF service in IIS along with the rest of your application.
The issue will be how to correlate the notifications with the page you're on in your MVC application.
I don't think this is specific to ASP.NET MVC really. If you have a WSDL for their web service, just use that to generate stub classes using either wsdl.exe or by adding a web reference to your project, then call the web service from your controller.
If I remember correctly SOAP is basically xml requests and responses.
You might want to look into WSDL (Web Service Definition Language) to avoid having to deal with raw data, and you would likely find a great deal of tutorials on wsdl as well.