why asp.net MVC controllers are not considered as RESTful web service - asp.net-mvc

I have an asp.net web application, now let say a user send a POST action method to the application using following URL Myapplication.com/Customer/Edit/120, then this URL will update the customer who have ID=120 with the data sent.
So this is the same as calling a web service, since you are integrating with the application (in this case Update a customer) using URLs, so why this is not consider as having a RESTfull web service!

What exactly is a web service ? And how do you differentiate between a web page and web service.
These are just different terms of retrieving some Resource. Either with Web Service or Web Page, you are just requesting for a resource. The representation is fixed for web page i.e its always HTML. If from my web service instead of returning Json or XML , if it start serving HTML would then make it a web page ?
I think the whole concept of web service came with SOAP world. With Rest there is nothing as web service or web page. There are mere different representation for a resource(json, xml, html, image).
Further to answer your
so why this is not consider as having a RESTfull web service!
Microsoft has launched Asp.Net Web API, which is nothing but pure MVC to serve content.

Related

same domain and one publish for web.api and mvc application?

I have a web api returning json and I have Mvc website consuming that web api. I want to make api calls on the clientside using Jquery, not in the mvc controllers on the serverside. Therefore I don't want to publish them as 2 different application with 2 different domains as it will cause cross site scripting.
ex. mywebsite.com for mvc webapp
mywebsite.com/api for web api
Ok I figured out how to run them under same domain as explained nicely here
But I believe all of these solutions requires deploying the webservice and web api separatly even they run under the same domain?
I want to publish only mvc webapp and api should be published together with it. is it possible?
for example something like "nopcommerce" project is doing with Nop.Web and Nop.Admin? When publishin Nop.web also includes nop.admin and nop.admin is reacheable with the url like nopcommerce.com/admin

Separating Web Api and Web Site

i'm new to asp.net web api, owin, and everything related to it.
I'm trying to find the best way to do this scenario:
1 - Web api to have all the connections and rest service
2 - Web site to show data to user on a browser using the restful service
3 - An mobile app that have some functionalities like the web site and access the restful service to get all the information
My doubt is: what's the best practice related to the login? I'll use owin/oath2 with Identity to login, but since it's going to be implemented on the web api, the login/register/forgot password should be on the web api directly (like the project template does) or should i move most of the functionality to the web site? Of course its easier to leave in the web api, but if i do it, i must duplicate my razor templates just to call the login part. Can someone give me a path to follow?
Thanks!
the answer is not, your web api should not have any html or js or css file, only the services that your need, the web api exposes the functions to register the user, next when you have to do request, you must Send a token, you can obtain the token using the URL that you have configure in owin, the URL is like /token and Send the username and pass.
Regards,

Can a web api sit on remote server?

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.

How does web API and MVC work together?

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.

Reference web api from a different project

I am developing a MVC app. I want to develop some web api's so i can use them from other projects.
Can this be done as currently when i do create a web api project it creates all the controllers and views etc.
Also would this be the place where you setup the types e.g. contact type which contains all the objects for a contact e.g. name, address etc.
Just need to get it right before i get my head into it.
Thanks
You could perfectly fine define your Web API controllers (deriving from ApiController) inside separate projects (class libraries). The Web API is normally RESTFul and Web API controllers don't return views. They return models which are normally XML or JSON serialized. You could even host the Web API outside of an ASP.NET application in its own host which could be a Windows service or Console application for example. You don't even need IIS. The following article described a self hosting scenario.
As far as the consuming client side is concerned, that could be absolutely any kind of application ranging from desktop application to another ASP.NET site. The client application could use the native HttpClient class to consume the WEB API or any other HTTP capable client such as WebClient or even HttpWebRequest. The client could even be written on some other non .NET platform. All that is required is the capacity to send HTTP requests.

Resources