Integrating MVC code into asp.net forms site - asp.net

I have an asp.net forms based site, and I am thinking of somehow integrating OpenForum which is written using MVC. I don't have any experience with MVC, so I'm wondering should I even bother - is it possible/makes sense to build this hybrid?

I guess you can add OpenForum like a SUBWEBSITE for the your MAINWEBSITE under IIS. It should work well because it is going to have its own BIN folder as well as web.config.

It's certainly possible. I've done it the other way around - added ASP.NET forms into an MVC site. And it may make sense (I've never used OpenForum), one could migrate to MVC this way.
If you search for "mix mvc and asp.net" there's a few resources with step by step instructions.

You can have some working side by side, but you mentioned in a comment about wanting to share master pages and user controls, and that's most likely going to be somewhere between difficult and impossible in some cases.
MVC doesn't use Viewstate which is a big part of webforms, nor does it have the concept of a postback. It's an entirely different paradigm. You can share session and also common classes and data structures, but as far as re-using view logic between them, it's most likely not going to be easy.

Related

Moving from WebForms to ASP.NET MVC

i know ASP.NET and i would like to learn and make a new project in ASP.NET MVC.
Is it easy to move from asp.net to asp.net mvc? Will the knowledge i have so far be helpful?
Im asking because i will have only few days to learn asp.net mvc and i want to know whether it's possible...
Cheers
I think you will find this questions (and its answers) very helpful.
Is it easy to move from asp.net to
asp.net mvc?
My answer to this would be a question back to you. Do you understand HTTP? Is web forms sort of magical to you, or do you have a firm grasp of what the abstraction is doing?
The reason I ask is that MVC sort of strips away a lot of the "magic" that web forms gives you in terms of treating a stateless protocol as actually having state. I tend to think of out of the box web forms development closer to classic windows forms development then normal web development. There are good and bad things about that.
What MVC is doing is bringing closer to what is actually happening in HTTP and in the end giving you more control and power over that. What you won't find in MVC is slew of drag and drop datagrid type controls, but it will make it easier to write AJAX heavy sites with common javascript libraries and clean html, as well as enable you to more easily write testable code.
Will the knowledge i have so far be helpful?
Without knowing what your "knowledge" is, it is hard to say. Obviously c# and vb are the same it's not like its a new language.
It really comes down to what type of web developer you were in asp.net forms. If you found yourself annoyed at how IDs were generated and how the html output of controls was horrible and that the framework was making it hard to do what you want then you should be fine.
If you were like I stated before, more of a drag and drop type of web forms person than MVC might be a bit challenging, but I would say its a good challenge and something that will ultimately make you a better web developer.
The only way you'll really know is if you try... There are tons of resources out there to get you started.
Hope that helps. Don't be afraid to ask questions on here as you go, there are tons of MVC devs on this site.
If you've ever used technologies like ASP classic, Coldfusion, PHP, etc. where you intersperse regular markup with server-side logic, then you will be able to pick up the "view" syntax pretty easily. The controllers are pretty simple to grasp once you get the hang of it, and the model shouldn't have to change from what you are used to. The hardest part for me to grasp when first learning ASP.NET MVC was the routing.
Like anything else, it will take some effort before you learn the "right" ways to do certain things, and you will initially be a bit less productive in the new technology. But in the end it is worth it.
Personally, I felt that ASP.NET MVC was far easier to learn than ASP.NET WebForms.
Run through the NerdDinner tutorial. It should take you about half-a-day. I think you'll find the simplicity refreshing.

Usefulness of asp.net MVC framework as opposed to coding MVC style with regular asp.net?

I am new to the asp.net world (but not new to .net) and I have been playing around with various things to see how I want to architect a web application I am developing. In my playing around I have decided I did not want to use asp.net's controls, as I have done enough php and ruby on rails to be decently familiar with regular HTML, and a decent amount of javascript.
I see a lot of comparisons between asp.net webforms and asp.net MVC, however when ti comes down to it I am not seeing that much of a difference (and I'll admit that it's probably me missing the differences rather than them not being there). I already have coded my asp.net application using a MVC type of architecture (aspx has the view, aspx.cs has the controller code, and models are separate classes in the app_code directory).
If I do not intend to use asp.net controls and just intend to use pure html and javascript, what are the advantages of using the MVC framework? As of right now, the only one that I am seeing is routing, and I'm sure if I look more I can find easy ways to do custom routing without using the whole MVC framework.
Anyways, if anyone could go into details of asp.net webforms minus the asp controls versus mvc framework, I would be greatly appreciated.
You're going to find a number of differences and reasons to use MVC over WebForms but it's going to depend on whether those differences are important to you or your project. Routing is definitely a big advantage for MVC. Although you can implement custom routing configurations in WebForms it's significantly less intuitive.
Another big advantage is unit testing. Along with an IoC container and a mocking framework, MVC makes unit testing a cinch. It's much easier to isolate actions and behaviors with MVC and test those specifically.
A third advantage is that MVC will help reduce the spaghetti code you're going to write. If you're not planning on using any User Controls then it won't be long before you're missing the HTML helpers in MVC. The Html, Url, ViewModel, TempData, etc make working with raw HTML much easier.
The inherent validation in MVC is also impressive. It's getting better in MVC2 and now with the integration with client side validation libraries it will save you a ton of time and add a lot of functionality.
No more VIEWSTATE.
There are many others but again it depends on whether those features are important to you. Good luck with your decision!
ASP.NET MVC is built on interfaces, which makes it extensible, easy to write unit tests for, and encourages clean, loosely-coupled architecture.
If you're using 'classic' ASP.Net without using any of the server-side controls, there's not much of ASP.Net that you're using -- it's basically classic ASP with .Net instead of VBScript behind it at that point.
Giving up webcontrols is the biggest reason not to use ASP.Net MVC. If you're fine giving that up, I'd just use ASP.Net MVC and be done with it. It's got all the parts you'd need to write yourself anyway, and the parts you don't like, you don't have to use.
In my playing around I have decided I
did not want to use asp.net's
controls, as I have done enough php
and ruby on rails to be decently
familiar with regular HTML
Thats not a good reason to decide against ASP.NET controlls. The importand thing about ASP.NET is Viewstate. It allows you to use event driven programming in Webapplications. This makes a lot of things very easy and productive.
Anyway - as you know ruby on rails I guess you will pick up ASP.NET MVC pretty fast.
First up, if you use ASP.NET MVC you'll be using a supported framework that has books, online resources and hundreds of blog posts surrounding it.
Second, I suspect you need to play with ASP.NET MVC to see it's power. There is way more than WebForms in ASP.NET MVC. For many, the fact that WebForms is optional (you can use different view engines) is a major plus.
For me, the extension points, the cleaner code, and the unit-testability of everything I do are major reasons to never go back to ASP.NET "classic".
My recommendation to you is to get a decent tutorial (look for the Nerd Dinner tutorial), play with ASP.NET MVC, and find out for yourself.
Note: ASP.NET MVC is not for everyone, and if you're heavily into the post-back mechanisms in ASP.NET, or have a heavy investment in server and user controls, then you might not want to take ASP.NET MVC right now. But for your next project you should at least know what your options are.

Do I need ASP.NET MVC?

I realize that ASP.NET MVC has all the hype. I have my doubts that I need it, but wanted to explain my potential project:
This is an internal LAN application. It is doing CRUD operations and a little reporting.
The user base is small (< 12 people) and there is not tons of data
There is not a huge number of screens (Maybe 20)
I don't care about URL rewriting
My view state is typically small (like a DealID or ClientID)
Even though I don't have a full mastering of Page Lifecycle, I do understand Postbacks and don't have a problem coding for it.
I believe in Layering and am familiar with the M-V-P pattern and other patterns.
I want to do some Unit testing, but heck 25-50% coverage is better then what most apps.
The app will have a little AJAX for search screens, but don't see it being overkill.
So what do you think? While using the "sexy" technology is cool, is it necessary?
No, it's not necessary. It sounds like you've already made up your mind. If the application isn't all that critical and you're more familiar with web forms, just do it the way you know how to. I think ASP.NET MVC is worth learning, but it isn't the right solution for every project. Go and try it out in your free time so that when this situation comes up again, you'll have more options available to you.
No you don't strictly need MVC but are you asking whether you should learn something new or use what you already know for an internal project? An internal project might be the perfect place to try out something new.
As was said, it's not necessary. However, if you're doing a basic CRUD application (and it sounds like you are), MVC would make that nice and easy.
You mention you're moving to project management. Do you think any of the projects or products you look into will be using MVC? If so then at least you'll have some familiarity by learning it with this small application. This means it is necessary, if you need it as a PM, but you do not require it just to build the app. It could run with anything you choose.
Incidentally, ASP.NET MVC is new, not MVC, not by any stretch of the imagination. I know you did not say it was, but you mention hype. I cannot help but think Java and other folks are saying, "We've been doing this for years and years."
You can use both.
Create a Visual Studio Web Application (not a website), and you'll be able to use both if needed.
You'll just need to add a reference to MVC and setup the routing.
Webforms and MVC
It's not necessary, no, but there's no reason why you can't use webforms or MVC.
If you feel comfortable working with webforms, then go down the webforms approach. If, however, you feel the need to broaden your knowledge of architectural patterns, then ASP.NET MVC is a pretty nice thing to use.
If unit testing is important to you, go with MVC. Everything else you mentioned, though, points to WebForms.

Using MVC for new parts of a Webforms project

I really like the MVC "way" and have actually enjoyed learning ASP.NET MVC (I never liked ASP.NET Webforms but I didn't know why until now). The problem is I'm about to inherit a bunch of Webforms code and wondered if I'll be able to add new things to the codebase with MVC instead off using Webforms. I suppose it depends a lot on how authentication is set up and if the code is set up in a way that I can include some library or something and just use those function or if I'll have to recreate them or gasp duplicate them somewhere else. What should I worry about? Should I just stop messing with MVC and get working on Webforms or can I make it work together so that if I need to modify something from the Webforms codebase I can just remake it the MVC way as I move along?
I would recommend attempting to only add new stuff using MVC and convert over the legacy webforms code as you can.
This is a pretty decent post of putting them both together in the same project:
http://www.chadmyers.com/Blog/archive/2007/11/30/asp.net-webforms-and-mvc-in-the-same-project.aspx
Scott Hanselman has a post that talks just a little about it in his blog. If you want to start going down the MVC route then go for it. I think it probably depends on how big the ASP.NET Webforms app is.
Well, MVC is built on top of Asp.Net so they should be able to play well together. The question is if the data is structured in such a way that it would lend itself to using the MVC framework and ideology.
Also think about the maintainability of it -- and not just for yourself, but for others. It might be better to just stick to webforms depending on where the application state is at. If you have to start doing more work just to get the application to work, it might not be worth the hassle.

Has ASP.NET MVC made Web Forms a Legacy Platform?

Last week at Mix '09, the final version of ASP.NET MVC 1.0 was released.
Some of the stated benefits of this framework are:
Clear separation of concerns
Testability - support for Test-Driven Development
Fine-grained control over HTML and JavaScript
Intuitive URLs
Now, Microsoft are being careful to tout this as being "an alternative, not a replacement, for ASP.NET Web Forms", but given the advantages mentioned above, I'm wondering:
How long will it be until "classic" ASP.NET Web Forms is considered to be a "legacy" framework?
If you were kicking off the development of a new .NET web project today, why would you choose to use Web Forms instead of the ASP.NET MVC offering?
Good questions. I think ultimately, the answer is going to be the development team's expertise and the project needs that will decide that. ASP.NET web forms is so heavily used that it likely isn't going away anytime soon. Plus, there are so many custom controls and third-party support such as components and books. The main benefit of web forms is how easy it is to get a dynamic website up and going. It really is a RAD way of developing websites.
However, once that team has more experience with creating larger websites with much higher demands in terms of scalability, reliability, and test-ability, then they will look towards other solutions for that. In this case, they will realize that web forms are harder for unit-testing. They may also see that viewstates reduce performance and look for possible solutions.
Although MVC has the stated benefits, it is unlikely that anyone will convert their sites to use this new framework right away or ever. Plus, it requires the team to learn the new technology, and work out the new bugs. The team will have to learn new ways to do the exact same thing. For example, how easy is it to support uploading a file using MVC?
As I saw recently, there isn't a reason you can't create a site using MVC and web forms together. So you may see more hybrids in the near future. But I doubt that web forms will ever go away.
I kind of think about web forms like the way VB1 changed the way Windows applications are created on the desktop. To this day, the RAD way of creating application still exists and will never go away.
Keep in mind that MVC STILL uses WebForms for it's default View Engine. Sure, you can replace it with another one, but WebForms is still a core part of it.
Also, not everyone prefers to tightly control the HTML or the Routing. That's not my attitude, but some people just want their job done with the smallest effort.
And aren't .asmx Files technically part of the "old" Model as well? I can say for sure that a lot of people would not like to see them go away.
Still, I personally see ASP.net MVC becoming the main Web Engine for ASP.net in the future, although not in .net 4.0 yet.
You're asking when a newly-released web platform, ASP.NET MVC, will replace Web Forms, which has been around for seven years.
If we'd been crying out for ASP.NET MVC for the past seven years, then it wouldn't have taken seven years before ASP.NET MVC was released. The fact is, not everyone sees a need for this. Many of us have been creating complex, highly-scalable web applications for most of those seven years.
We even knew how to make them testable, and to separate presentation from business logic and data access. ASP.NET MVC may enforce this separation, but I've done it by using coding standards and code reviews, and by saying, "there's no unit test for that", and "get that business logic out of the UI".
Also, if I really needed more control of the HTML, I would write my own control to generate the HTML.
I do not believe WebForms will ever retire.
I've been using WebForms at work in business applications and MVC at home for some private things. Though I really like MVC I do not see how this could be possible to implement really complex UI logic with HTML/CSS/JavaScript. It will quickly become unmanageable and will be quite unsecure since JavaScript can be switched off to prevent disabling some controls or hiding some information. On the contrary, turning off JavaScript with WebForms will virtually turn the page dead for any action, either authorized or not.
Both platforms will continue to evolve. For general web sites and HTML/CSS lovers MVC is a way to go, with complex applications you would want object-oriented architecture and artificial event handling even though it abstracts you from the stateless nature of HTTP.
So, pick up what is best for you.
P.S. Dropping WebForms altogether will jeopardize the future of numerous projects and companies throughout the world. Microsoft folks would not want to become an object of hatred and the trigger that started the third world war.
WebForms will still have a place for those that want a pseudo-stateless web application that they can easily put together by dragging and dropping. For those that don't have to or want to understand how HTTP works. It's the ultimate in RAD for web applications.
ASP.NET MVC on the other hands allows much more finer control at the cost of more responsibility. You get complete control over your HTML however that means you have to sanitize/encode your output yourself. Your application for the most part has to be completely stateless and for some ASP.NET WebForms/Windows WinForms developers that it's a bit hard to wrap their mind around.
I don't think either will ever dominate the other though one may be favored.

Resources