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.
Related
I am creating a fresh ASP.NET MVC4 website using a proprietary API that tells me I need to include <trust level="Full" legacyCasModel="true"/> in my Web.config file. When I do this, however, the application exceptions out with the message "Dynamic operations can only be performed in homogenous AppDomain" I have poked around a bit and it seems that this has to do with some dynamic calls not being allowed.
My question is: Does that mean I have to abandon MVC altogether and make my site with Web Forms?
Perhaps you can move the code which calls the API to separate DLL, make your calls there and expose that DLL as a web service (or .NET Remoting or whatever works). Then, from your MVC web-app, call your new web service in order to reach the proprietary API.
Basically, wrap the API calls in a (local) web service call.
This approach would allow you to use MVC instead of WebForms. It does add some complexity and overhead, but I think the pros (ability to use MVC) outweigh the cons.
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 an asp.net mvc3 application built with RavenDB and I want to be able to access the data via an external HTML5 mobile app. I'm thinking of exposing methods via WCF or via MVC controller action methods? Which option is best?
Ok, I have faced similar situation a while ago. This the way I have handled it, I have directly exposed Controller urls to the mobile application clients. Bascially it will help you in reducing the burden of maintaining two code bases and helps you to reuse existing functionality. Even if you go with WCF you need to expose with REST to make HTML5 client developer life easy.
This is the why microsoft released ASP.NET MVC 4 Web Apis to avoid confusion among developers which way to go in these scenarios. So that your services are device agnostic and easily testable.
Since you've already built the app in MVC3, I'd recommend a JsonResult action on an MVC Controller: http://www.asp.net/ajaxlibrary/jquery_json_data_from_controller.ashx
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
A Guide to Designing and Building RESTful Web Services with WCF 3.5, this article explains the foundations of REST and how it relates to WCF. MVC uses REST as the architectural model. I am guessing one can use the .NET MVC to create web applications that have both a front end and an API point, but I am not sure if the safe way of building the API is to build it with WCF and then use it in the MVC as a controller.
Please comment if the question is not clear, I will add or modify the text.
Theres actually a third option, ADO.NET Data Servies. Anyway, here how I see them.
MVC REST: Gives you full control over how to expose your data, you have to write all the code to get it up an running tho, e.g. serialization, deserialization, all the CRUD methods etc etc. Worht metioning that this being an MVC site means you are limited to exposing your service via IIS over HTTP(S)
WCF REST: More automation than MVC, a much more solid frameowkr than MVC REST, i.e. caching, security, error handling etc (basically all the stff you'd have to write yourself using plain MVC). Being WCF, you can host this in a variety of ways (e.g WS-, TCP) etc.
ADO.NET DATA SERVICES: The quickest way to get up an running with everthing ready to use, all you need todo is configure the global.asax, however you have to use an Entity Data Model, which you many not want to.
Personally, I would use either ADO.NET DATA SERVICES or WCF REST to build an API, consue that API in MVC site and then expose that API either directly, or by passing it through another layer.
ASP.NET MVC can serve as a REST endpoint for light services work, so I guess the answer to your question depends on how you define "safe."
Clearly WCF is designed specifically for creating REST endpoints, with all of the security implications that are implied thereof, whereas ASP.NET MVC is designed to create REST endpoints which can be used by ASP.NET MVC itself.
The following article shows how to create a web service using an ASP.NET MVC controller:
Create REST API using ASP.NET MVC that speaks both Json and plain Xml
http://msmvps.com/blogs/omar/archive/2008/10/03/create-rest-api-using-asp-net-mvc-that-speaks-both-json-and-plain-xml.aspx
See also the following article from Phil Haack, which discusses an SDK the WCF team put together for users of ASP.NET MVC:
Rest For ASP.NET MVC SDK and Sample
http://haacked.com/archive/2009/08/17/rest-for-mvc.aspx
They are two different sets of technologies, only related by being built on .net
MVC is used to create websites and provides a model where URLs are routed to controllers and controllers deliver views to the user as the user interface.
WCF is a set of libraries in .net that are used to abstract the type of service (is it hosted in a windows service, as a webservice in IIS etc.) as well as the protocol (HTTP, TCP, MSMQ etc.) from the client and server which are communicating.
An MVC website may use WCF to connect to a web service, but that is just one of many options.