I'm trying to learn wcf on practical example. I followed scalable wcf solution tutorial and my service works ok, client from my console app. works ok. But what I want to achive is consuming my service trough js from mvc view (razor) page. On my client console application I'm accessing to proxy with
IService proxy = new ChannelFactory<IService>(Configuration.MyServiceActiveEndpoint).CreateChannel();
List<MyObjectDto> data = proxy.GetMyData();
...
how to practicaly achive this creating proxy client from mvc view page (without adding service reference). Thank you
You don't do this.. you really really don't. This completely breaks the entire point of Model-View-Controller (MVC). The controller should be the one accessing the WCF service and returning the data to the View.
Check out servicestack.net for the cleanest and best web service implementation in .net. No config, easily callable from jQuery, and returns json by default. Easy to get started with NuGet Mvc 3 package.
The tutorial you are using already registers an endpoint with the enableWebScript behavior - you should get a js proxy generated automatically when you access http://server/virtualdirectory/X.svc/json url. Include that js file in your mvc view.
If you host the wcf service in the MVC web app you can use Url.Content:
<script src="#Url.Content("~/X.svc/json")" type="text/javascript"></script>
Then just invoke the service from js - use the javascript from this post as an example: http://dotnetslackers.com/articles/ajax/JSON-EnabledWCFServicesInASPNET35.aspx
Related
I really stucked in MVC + Angular 2 project in the first Step. The problem is, How to connect controllers and actions with Angular 2? Is there any routing needed?
I have done with all setups with Visual Studio 2015 to work with Angular 2. And it successfully works. But i really doing my projects in ASP.NET MVC 5 (Not ASP.NET Core). Here i dont know how the Controllers and Actions work with Angular. How can i route (navigate) to several controllers and actions in my project.
Since MVC consists of several controllers and actions. There will be many views for several actions and for every action there will be a GET(View) and POST(Form Submit) method also.
If anyone work with Angular 2 + ASP.NET MVC let me know how the Controllers and Actions are getting connected.
It looks like you are building your first SPA that communicates with a REST API.
For starters, you should get two things straight:
The ASP.NET MVC is your REST API. It will expose endpoints that can be requested via HTTP calls.
Your Angular 2 is the SPA (Single Page Application), which will make HTTP calls to the REST API you wrote in ASP.NET, and that API will give back responses of whatever you want. You can then use that response data to your liking in your Angular 2 frontend.
That being said, the first step you want to do is make sure you can properly access your API endpoints. Download a tool called Postman and test this with ease.
Ideally, you want to make this API a JSON API, because that will be the easiest thing to work with for your frontend. Plus, it's basically the de facto standard. But as always, use what works best for your use case.
Once your endpoints are working as intended, what you need to do is make some AJAX calls in your frontend that will hit those server endpoints. For Angular 2, you can use the Http package.
You will undoubtedly come across a CORS (Cross Origin Request Sharing) browser error when making the calls to your server. This is basically the browser refusing to send requests to your server until your server explicitly says you are allowed to make requests to it, assuming you are not on the same host and port.
The above was a really brief and limited-detail breakdown of what you need to do. For an example of putting it all together, check out my Angular 2/Golang chat room app. As you will see from this file, I expose some endpoints to allow create/read/update/delete actions for Todo's in my Go server. And the Angular 2 code is in this directory.
One last tip: If you are serving your front end files and exposing your API endpoints all on the same ASP.NET server, as opposed to having one ASP.NET server for the REST API, and one for the serving of UI files, then two things are true:
You need to have a catchall route in the server that will serve your index.html. You can see an example of this in this file. You'll notice a long that starts with r.NoRoute...that is where I say "no route was found so far, therefore serve index.html and let the Angular 2 handle the routing from there".
You no longer need to worry about CORS errors
Hope that helps!
Not actually, you will create and Expose APIs (WEB API) in your MVC project and your Angular 2 will consume those APIs.
It's more a structural question than a technical one.
I made a classical Web App with ASP.NET MVC.
I works well, the server responds with HTML when I send him an URL. Ok.
I now want to make a mobile application (Android) to access the same data.
Maybe there is a way to use the controller's methods which already return the objects I'll need.
So the question:
Is there a simple way to make a REST API from a ASP.NET MVC WebAPP?
Once again, I think that the controller's method will be pretty much the same. It just has to not return HTML but XML for instance.
I'm a newbie in the Web services technologies.
Add webapi project to your solution
Configure web api controllers
Reuse MVVC classes to access database
Return necessary data - webapi will return it in json and in xml (depends how client will consume it)
Using web.API with MVC seems very easy to setup, you leave the WebApiConfig.cs as default, create a new web.API controller, and then in your javascript you can call $http.get("api/...").
I want to be able to do the same using service stack instead of web.API. What makes it slightly more difficult is that the service stack api is in its own project. At the minute I can call the endpoints within MVC using the jsonServiceClient. However calling client side with Angular requires me to call the full FQDN which causes cross site scripting issues.
How can I configure my MVC project so that I can call my service stack endpoints from AngularJs's $http.get method like this:
$http.get("api/...")
rather than:
$http.get("http://localhost:2540/api/...")
You can accomplish this by creating an alias in IIS under your web project and naming it API and pointing to your ServiceStack project.
You may run into issues in the future with this if you need to scale out your solution, but as long as you don't need to do that, you should be ok.
How does MVC and web API work together in an application?
Are you supposed to use javascript binding (with knockout.js and ajax calls) and just use MVC for the actual container page?
Or do you call web API from the MVC controller (like a WCF service layer)?
The integration of MVC and web API just isn't clear to me.
Should I use Web API if I regulary require HTML to be returned (i.e. I want to work with Partials)?
I'd like to use web API so I can scale my app though (return HTML from one side, but remain with an API that can return/process XML)!
Thanks for clearing it up :)
This picture below from the link explains clearly how MVC and Web Api work together:
Technically, you can call from MVC to Web API, but it is not really the best practice since:
Calling from MVC to Web API will across the network (distribution), this makes your application more complex.
Web Api is REST Api, it is not like WCF which is heavily SOAP Api (although WCF support REST Api). So, from JavaScript you can call the Web Api easier using ajax.
Here's how I set up my latest MVC and Web API application: There's the regular model layer (*M*VC) and I have created a service layer for the business rules. The controllers of my MVC application call the service layer to process data from and to the views.
My Web API application is a external interface to the methods on the service layer, therefore both the controllers on the MVC application and on the Web API application call the service layer. I hope this helps.
I have asp.net application which i am planning to convert into asp.net mvc app.
One area that i havn't found how it could be done is, App is using scriptmanager to make ajax calls to wcf service. And scriptmanager simply takes care or all element related to ajax, serializing - etc.
I haven't found what i can use in mvc that replaces that piece of functionality
Server hosting WCF service
MVC calling wcf service through ajax?
No controller, view etc to wrap wcf service to respond for ajax call
I found lot about using jquery, ajaxaction stuff but this don't look like exact replacement of scriptmanager like functionality.
If I understand your question, the easiest method would be to continue to use the ScriptManager control in the MVC port of the application.
The Polymorphic Podcast did a show on this topic. There are several potentially helpful links in the show notes.