Combine classic ASP.NET and ASP.NET MVC within one web application - asp.net

We currently have a classic ASP.NET web application which serves as our API. It basically has a bunch of ASMX Web Services, but these are really cumbersome to work with from JavaScript. What I want is to expose a set of RESTful endpoints, but doing this in classis ASP.NET is not what I really want to do.
I'm considering to combine both classic ASP.NET and ASP.NET MVC in one web app. Is this possible at all? If yes, what are the issues/problems I may encounter?

Aha! It seems that this is very much possible. Here's a comprehensive article which describes what should be done.

An ASP.NET Webforms application can become ASP.NET MVC enabled by following some simple steps,its quite easy infact see this link
if you want to go rest for mvc heres a article Rest for mvc by Phil Haack and Rest like nature of MVC heres a comparison Rest in asp net vs wcf

The whole goal of routing is to break
the one-to-one association between
URLs and files in the server’s file
system. However, the routing system
still does check the file system to
see if an incoming URL happens to
match a file or disk, and if so,
routing ignores the request (bypassing
any route entries that the URL might
also match) so that the file will be
served directly.
So, to answer on a part of your question "is this possible at all": Yes, because routing system will recognize the .asmx file on the file system and it will process it in classic asp.net web services manner.
For the second question I'm not sure because I haven't been doing anything complex with web service inside of the asp.net mvc application.

Related

Simple plain ASP.NET application example (without Web Forms and MVC)

For many developers ASP.NET and ASP.NET Web Forms are the same things. For some time I've been using Web Forms, but recently I learned that Web Forms is only build on top of ASP.NET. I become more curious how does a simple plain ASP.NET application look like. I learned about 3 ASP.NET programming models: Web Forms, Web Pages and MVC (Here Scott Hanselman explains briefly the differences). But it still seams that all the 3 approaches sits on top of ASP.NET.
I was trying to find an example application which uses pure ASP.NET, but I couldn't find anything.
I started to dig deeper into source code of ASP.NET web page and I found out that each web page is actually HTTP Handler (each page implements IHTTPHandler interface). Does it mean that pure ASP.NET application would be just a HTTP Handler implementation? Or am I missing something.
In my understanding ASP.NET Web Forms correspond to Win Forms in desktop world. But in the desktop world you can still write console application which doesn't use Win Forms. So what is an equivalent of console application in the web world?
Any comments or references appreciated. Thanks in advance.
ASP.NET is a very broad set of technologies, and it includes WebForms.. and as of MVC5, it also includes MVC, WebApi and WebPages. This is what Microsoft is now calling the "One ASP.NET" initiative.
These are all part of ASP.NET, just like ASP.NET is part of .NET. These are all just layers. There is no 'seperate' part called asp.net that does not contain these pieces (and even in earlier versions, there was no version of asp.net that did not include WebForms).
As such, there's no such thing as a "pure asp.net" app. You can certainly write an app that does not use WebForms (which means no .aspx or ascx or .master files, no Server Controls of any kind... essentially anything that exists in the System.Web.UI namespace.)
At most, you could consider this whatever is in the System.Web namespace (without any further levels of namespaces. Essentially what's here http://msdn.microsoft.com/en-us/library/system.web.aspx )
So that would be things like the Request object, the Response object, Cookie handling, Membership, FormsAuthentication, etc... This is basic plumbing that isn't all that useful by itself. You would have to write a presentation framework of your own to sit on top of it.
There are other frameworks out there, such as FubuMVC, Nancy, MonoRail, etc... these are just like MVC or WebForms or WebPages... using the basic ASP.NET classes to do their job.
I would like to refer you, as an example, to ASP.NET MVC.
ASP.NET MVC is a framework on top of ASP.NET but is definitely not Web Forms (in fact, it replaces it completely, and to my opinion - for the best).
ASP.NET provides services common to web in general, things such as:
Handling requests and responses
Security, Authentication, Impersonation etc.
Maintaining an 'Application' (the notion of Global.asax for example as a framework around instansiating apps, shutting them down, handling central things such as Routes, handling uncaught exceptions etc.)
Taking care of hosting permissions
Physical & virtual apps
Interfacing with IIS (or other web servers) in general
Running Modules (in addition to HTTP Handlers)
Web Forms adds a presentation layer on top of ASP.NET's infrastructure. It provides things such as Forms, ViewState and such. "Translating" code-behind code to HTML representation for example. This are just few examples.
The separation between ASP.NET as an infrastructure and Web Forms as a presentation layer allowed creating ASP.NET MVC later on. ASP.NET MVC does not rely on 'Forms' at all, does not try to mimic WinForms development workflows (by obscuring raw HTML using code-behind "methods" and "properties" and such) and in fact provides a much better & correct way to do web development.
You were asking about pure ASP.NET. Here is an example of pure ASP.NET, no WebForms, no MVC. Here is one: http://support.microsoft.com/kb/308001 . It's a basic HTTP Handler that, in this example, runs every time your browse to some address ending with .sync (by "teaching" ASP.NET to handle every request that uses that extension).
By the way, you can treat it as a "console" app (in your terms). It gets a URL, no matter where from (you can use curl if you like) and returns a text response, and that's all.

Is there an equivalent to ASP.NET Web API in the Rails world?

Or is Rails by itself just good for developing APIs?
It seems that ASP.NET Web API project types have some history intertwined with WCF (as is detailed in this post for example), so maybe it's not applicable to Rails.
UPDATE
To clarify, Microsoft has the ASP.NET MVC framework. Recently, they came out with a framework called ASP.NET Web API. ASP.NET Web API seems to have similarities to ASP.NET MVC, but is specialized and trimmed down for RESTful web services. Is there an equivalent in the Ruby/Rails space?
So, the answer is Yes to both questions. Rails does have an equivalent and its Rails.
ASP.NET Web API looks like at it's heart is just a RESTful router with type negotiation. I could be totally off base here, but from the tutorials I saw that's what it looked like to me.
So yes, from what I can tell, Rails supports most of the things that the Web API was created for. In fact in Rails most of this stuff is forced onto you until you become informed enough to be able to change it (assuming by that point you know better than to actually do that).
But, as far as Web API functionality. That really comes from the ability to support HTTP verbs (GET, POST, PUT, DELETE) which Rails does.
But a source of confusion might be that in Rails the RESTful API is actually the application itself. Meaning you don't need to implement any other libraries, it's just built that way.
Here's a quick example of that I mean
When you hit /users/1 you will get the data associated with that user depending on the format you requested. So if you request JSON the controller returns JSON, HTML you get HTML, XML you get XML, etc. (As long as said format is implemented for that resource)
A good overview of what I'm talking about are in these two sections:
Rails Guides::Controller: Rendering xml and json data
Rails Guides::Routing: Resources on the Web
So you could build a website, API, or both in a Rails app and they would all start the same way.
But from my limited knowledge on the matter, I would say a ASP.NET MVC with ASP.NET Web API program is actually a lot more like a Rails Program than the regular ASP.NET MVC programs that came before them.
Or it's all just a clever ploy to get as many Capital Letters in a title as humanly possible.
Take a look at grape. It is a pure "Rest" HTTP API framework in ruby.
WSO2 looks like a generic web services framework (as opposed to MVC like Rails) I can't vouch for it but it seems to be more a service framework in the style of WCF Web API (service in the generic sense, not just SOAP).
It's difficult to know what you mean by "APIs"... Rails and ASP are used for developing web sites, and WCF is essentially a web service platform. ASP and WCF have little in common, it's just normal for ASP applications to consume WCF services because they all run on the same stack and platform.
I suppose Rails on the Microsoft side would be a combination of ASP.NET MVC, Linq2SQL or EntityFramework and some WCF.
Ok, this isn't a direct answer to your question, however there seems to be some confusion... Microsoft's ASP.NET Web API is specifically a product offering with ASP.NET MVC 4+. It is a RESTful framework. How does it compare to RoR? I don't know having never tried to install RoR on Windows. As with anything else, experiences vary... Requirements vary. Also try to think ourside the language, construct, context, and framework. Is it better for developing API's? If you're using Linux/Unix, the answer is probably a yes. If you're on a Windows server, the question is a bit trickier.
Finally,
Writing in the ASP.NET Web API will have 0% to do with WCF. Perhaps it is implemented as such under the covers, but the ASP.NET Web API is (from what I've seen and done with it) strictly an HTTP bound API, not TCP/Binary/Piped/etc... like WCF. If you're ask
Yes. It's called Grails. It uses spring. There are tons of plugins available for it and it make creating webapps a breeze. Read more about it here.

Need to develop a RESTful API (both JSON and XML)

I'm looking to make a RESTful API on ASP.NET for a website. My problem is that I need it to be integrated into the website and not as a separate project.
I understand that WCF makes this really easy and its the ideal way to do it, but I don't think you can combine a WCF Service Project and a ASP.Net Website, Is this correct?
Is there a way we can do this using a webservice (asmx) file (since I know that asmx services use SOAP no?)
The reason I need this to be in the same project is that the customer will be able to purchase ssl for their domain (which the website is going to use) and I need to make the API secure as well, but the customer should not be asked to purchase two ssl or even a wildcard one.
Knowing this, is there a better easier way of doing this using WCF?
Take a look at the new MVC4 Beta, it's set to go live sometime between March and April this year and should be able to accommodate your requirement to build a RESTful web service alongside a web application. I haven't spent too much time with MVC4 to go into the details, but it's definitely worth a look. Links: Get MVC4; MVC4 and WebAPI blog.
Hope this helps!
You can use ASPNET MVC to build an API along with your website.
See How can I implement a site with ASP.NET MVC without using Visual Studio? for some details on building a basic MVC site.
ASPNET MVC services can respond in JSON or XML, or both.
There will be no special requirement for two SSL certs.
I have an ASP.NET MVC 3 application that exposes both WCF REST services. I'm using .NET 4. You'll have to pay attention to how you configure your routing. For example, my WCF services are prefixed with something like "api/v1/" while all other requests are handled by ASP.NET MVC 3.
I had a problem because IIS refused to serve some "localhost" requests (like when your MVC 3 controllers try to consume your WCF rest services). That was solved by adding an entry to my hosts file. Also be aware of this when implementing an OAuth 2.0 Resource Server or Authorization Server.
Using WCF for REST services works ok in .NET 4, but the JSON serialization sucks big time. There are issues with default dates and it is rather slow. You may want to look at using a different serializer. With WCF you sacrifice some flexibility for some features you get for free.
ASP.NET MVC 4 (and the WEBAPI) is still in BETA, so I'd avoid that for a project with a short term release date.
I'd actually use NancyFX. Setting up routes is super-easy, and it comes with built in XML and JSON serializers that act based on the data in the headers.

Asp.Net MVC and Web Services

I have an existing Asp.Net MVC Website and I would also like to provide a Web Service from the same domain.
What is the best way to approach creating a web service in this scenerio?
Do I add to this project or...?
You should be able to add an WebService file directly to the MVC project.
Right click on solution and select add new item, then select the web category and att the bottom of the list there should be Web Service.
Just remember to check that the routes does not eat up the call to the webservice.
That way the webservice can get access to the same model classes as the MVC application.
You can add a web service to the project just as you do in regular ASP.NET web apps, however, MVC basically IS a web service. You could create a controller that handles all the requests that you want your web service to handle.
With the advent of MVC it is quite common to do applications that only ever load a view once, then use AJAX and client scripting almost the entire rest of the life of the application. Your AJAX calls just hit up action methods for their goods and then use the deliciousness that is JSON to parse the data and utilize it.
In my opinion designing a web service as a controller instead of using [WebMethods] is far simpler and a lot more fun!
First, the question is "what do we mean by web service?" This can mean anything from a MVC page that responds using XML, JSON or some other agreed upon format to full blown SOAP and WS-* encumbered nightmares.
Anyhow, perhaps the best place to start is the WCF restful services -- these play very nicely with MVC, including routing.
The cool kids are using openrasta.

ASP.NET MVC & Web Services

Does adding a Web Service to my ASP.NET MVC project break the whole concept of MVC?
That Web Service (WCF) depends on the Model layer from my MVC project to communicate with the back-end (so it looks to me like it needs to be part of the MVC solution).
Should I add this to the Controller or Model layer?
It sounds like you should split out your model into its own assembly and reference it from your MVC-application and WCF-application.
YourApp.Data -- Shared model and data access maybe
YourApp.Web -- If you want to share more across your web-apps
YourApp.Web.Mvc
YourApp.Web.WebService
If you want to do WebServices MVC-style maybe you should use MVC to build your own REST-application.
Is there a specific reason you need to add web services to your MVC application? Unless there is a specific reason you should use your controllers in a RESTful manner just as you would a RESTful web service.
Check out this post from Rob Connery for more information:
ASP.Net MVC: Using RESTful architecture
Separating the Model into it's own project is not breaking the "MVC" pattern. First off, it is just that -- a pattern. The intention of the MVC pattern is to clearly delineate between your data, the data handlers, and the presenters and the way you interface between them. The best way to do it is how Seb suggested:
YourApp.Data
YourApp.Web.Mvc
YourApp.Web.WebService
Something that might help you out is the MVC Storefront that Rob Conery put together. Go watch the video's here:
MVC Storefront Video Series
And if you want to look at the actual code in your browser to quickly see how he did it, go here:
MVC Storefront Codeplex Code Browser
I don't think separating the model into it's own assembly has any bearing on whether or not you're using MVC, you still have a model. Where it is is irrelevant surely?
I've had a go at doing this.
See my result at my blog
ps: I don't believe that this will break the MVC concept so long as you think that a web service is the model of a repository because all a web service does is returning a XML dump.
I have added web services to my application and it works well. I don't believe it violates MVC because it is an alternative interface to your model. MVC is not appropriate for web services because web services don't have a view.
Think of web services and databases as one in the same. Under this analogy, I think it makes sense to place your web service ingteractions where you place your database logic.

Resources