Relation of webforms in MVC - asp.net-mvc

I am a beginner to MVC technology and have few questions.
1) How can I webforms in MVC and why/when should I used it ?
2) If we can build a application using razor view engine then why webform engine is provided ? If it is, we can continue to use traditional web forms.
3) Can I combine razor/aspx view engine in one project.
Coz I can see we can add WebForm/master pages by right clicking in View folder ....
Any help on this appreciated.

Well I'll recommend you reading some ASP.NET MVC book instead. Because it will give you basic understanding of MVC concept. But still:
You can't use webforms in MVC. Because it's completely different approach;
MVC gives you more flexibility over markup, you can customize your application easily. Again read about MVC pattern. Webforms instead are faster to develop, but heavier in terms of size and have some ugly things like viewstate;
Yes, you can. But in different pages, not in the same page.

Related

Net5 razor pages vs mvc

I am new to net5 framework and have some questions regarding the structure of the project.
When we create a new project it will only create the razor pages. I am learning that we can achieve things with these razor pages.
Meanwhile, I have looked into some open source net5 projects on GitHub and see that they are also using MVC.
So, my question is why do we need MVC then? If everything is achieved using razor pages then what is the need for MVC?
If we need MVC then should we use razor pages with them too?
I have done the identity work with razor pages using scaffolding. What would happen now if I add MVC to my project. How will I manage the routes and page redirection from razor pages to MVC and vice versa?
Please help me with this so I can clear my concepts on this.
I will be really thankful to those who explain the scenario to me.
Thanks
First, .NET 5 is out of support. You should target .NET 6 instead.
Razor Pages represents a simpler way to generate HTML on the server compared to MVC. It is recommended for all new web applications that rely on server-side HTML generation going forward. MVC is still available for existing apps. It is also probably easier to migrate older MVC 5 (.NET Framework) apps to ASP.NET Core MVC when porting to .NET Core.
Razor Pages is built on top of the MVC framework and depends on a lot of the features of MVC such as model binding, action results and so on.
If you are using Razor Pages, there is no compelling reason to also use MVC in the same project, although you can. If you do, then you need to register controller endpoints:
app.MapControllers();
You might need to add controllers to your project if you want to create HTTP service APIs, although you can use the minimal API feature that was added to .NET 6 instead:
https://www.mikesdotnetting.com/article/358/using-minimal-apis-in-asp-net-core-razor-pages
Razor Pages can make coding page-focused scenarios easier and more
productive than using controllers and views.
https://learn.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio
As another difference, in MVC, you usually create separate class for ViewModel. As I understand, in Razor Page, you don't need a separate class and you just add more properties in the same page and those properties will be part of your model which make things easier.

Using ASP.NET MVC under ASP.NET Web form project - Best practice

Currently we have a new menu item to our existing asp.net web forms application.
We have decided to go with MVC for this.
Please note that we have to share the same masterpages, css and Jquery files. We are currently planning to render the view inside a div in our aspx pages.
Is there a better way to accomplish this?
What are the limitations of our approach?
Can we leverage the MVC test cases in this approach?
Thanks
MVC is a stateless, disconnected architecture, you can not access the master page or aspx controls directly in the controller as aspx.cs does, secondly it is not possible to use the classic asp.net master page with mvc.
The things you can re use from existing project are:
UI Templates (design)
JS files
CSS files
Business Logic
Database
You can't use same master pages. You can't reuse ASP.NET MVC for some parts of your webforms view and vice versa. Either your view is completely on webforms or on ASP.NET MVC. And theoretically even mix of webforms with mvc in one web application could be problematic, but practically possible (see below point 1). So generally answer to your question is no.
If you are looking to ASP.NET MVC, you should think about step-by-step migration of your project to it.
Actually there are a couple of possibilities how to migrate
Mix in one project ASP.NET web forms and ASP.NET MVC. Technically it is possible. But it is some kind of hack. Your transition will be seamless but you can't your ASP.NET views. I will not recommend it. You can find this approach here http://www.devcurry.com/2013/05/adopting-aspnet-mvc-enhancements-in.html
In one solution use different projects for your webforms and mvc projects. In fact these are 2 different web application which typically could have common authentication, and there is question with webforms state. To solve problem with authentication you need create separate web service for it, which will be used by both. To solve problems with session state, use distributed caching for both, and try to change thinking from webforms state to caching, because mvc is actually stateless. Then for particular views you can redirect between views of both portals

Migrating a webfrom asp.net appllication to MVC

I have been working on a CMS project based on asp.net webforms. It follows simple multitier architecture and contains a single aspx page where the usercontrols (ascx) in the form of modules are loaded dynamically.
Now a lot of clients are demanding it in MVC. I m wondering if it is feasible to seamlessly convert it into MVC. Is the hype just because MVC is the new paradigm for dot not or is it because it does yield substantial benefits?
What is the easiest way to migrate from WebForms to MVC?
You can always convert your existing ASP.NET web application project to include MVC and make it as a hybrid application. Scott Hanselman wrote a simple post to demonstrate how to do that.
If you are trying to add MVC to existing ASP.NET web application, Check out this link. I have scribbled a useful tip there to bring the MVC specific Context menu ( Add Controller/Add View) in the solution explorer.
The below statement is purely personal. (of course both has it's own pros and cons)
Personally If i start a new project, i will choose MVC over webforms. because i tasted it. It is addictive. :) I think My MVC projects are much cleaner than webforms ones.

Incorporating MVC functionality in an existing ASP.net project

We have an intranet system developed using asp.net webforms. We are kind of planning to partly incorporate mvc and such a scenario is as follows,
We would like to generate html documents using mvc (using razor view engine) where the inputs are going to come from a normal asp.net webform (from a form button click or so)
Some pointers or references to tutorials on running these 2 scenarios side by side would help a lot.
ASP.Net MVC and WebForms postbacks don't mix; the only way to do this would be to use <iframe>s (which is not a good idea).
I am not sure about your described implementation... however, you could start by re-writing some of your aspx pages using MVC views and just get all the existing code behind into a controller to minimize the creation of new code. You could do this one page at a time until all of your pages are MVC views. Good luck.

What is ASP.NET MVC not good for?

I'm a big fan of what ASP.NET MVC is doing, on many levels.
I'm about to take part in re-build of a very highly trafficked website, and I'm not which framework would be best (if any).
The site will need the following:
To support Javascript-heavy, highly interactive pages
But at the same time, provide underlying semantic HTML for search engines
Support multiple languages
Be skinnable
Expose a RESTful web-service API for partners
As far as I can tell, there's no reason not to use ASP.NET MVC for this.
I can present semantic HTML and layer Javascript on top using jQuery.
Multiple languages can be catered for using Resource files (same as at present).
Skinning can be done with CSS (it won't involve changes to the markup).
I can centralize business logic so that the Controllers and the WCF web-service use the same code.
But are there potential drawbacks to using MVC that I haven't considered?
I don't want to be the guy who picks a technology because it's cool but finds later down the track that it isn't very suitable for the job.
ASP.NET MVC is not good when all your doing is making a website that needs server-side code (but that's also true about ASP.NET also).
In your case I think MVC would be a great way to go. MVC has proven itself on high traffic websites (e.g. this one). However you must remember that MVC is new and changing. A library may not exists to do a specific task which means you'll have to write that code yourself.
Good luck on your rebuild!
You're good to go with MVC given what you've said about your project.
As far as I'm concerned, ASP.NET MVC is really only NOT good for situations where you have a large codebase in WebForms (meaning you have a lot of ASP.NET user controls, custom controls, etc). It's also not good if you are going to have people working on it who don't know what it's all about. Other than that, it's a pretty nice technology.
My two cents:
ASP.NET MVC is a great option but there is a little learning curve involved, so make sure your project plan/timeline has this handled. There might be developers on your team who might not be comfortable working with ASP.NET MVC, and this can cause possible delays (a lot of developers are still working in ASP.NET 1.1!).
#Alex: Lack of controls. Some features (like TreeView or Menu) are already implemented as Controls and it would be waste of time to reimplement them using mvc.
IMO the idea of using controls in ASP.NET MVC doesnt make much sense. You can create a treeview control using jQuery easily. Classic ASP.NET server controls carried a lot of baggage (viewstate etc) and hence ASP.NET MVC did not use any of those controls (though you can use helpers).
Finally, ASP.NET MVC is an alternative, not a replacement to Web Forms. I would not use ASP.NET MVC as it is still evolving, and my team is not very comfortable with it, but I guess slowly more and more programmers would shift to this (better) option.
I do not like ASP.NET MVC because of following reasons:
1.
Ugly routing API, there http://ayende.com/Blog/archive/2008/11/05/a-case-study-of-bad-api-design-asp.net-mvc-routing.aspx
is description of what is wrong.
By the way, friendly urls can be easily implemented without mvc
http://demo.liveui.net/bugtracker/Tasks/7
2.
Poor object model. It is proved that good software should consist of reusable components. There is nothing that can be reused in ASP.NET MVC based web site. For example, if you implemented smart drop down list once it will be difficult to use it again (even on the same web site).
3.
Lack of controls. Some features (like TreeView or Menu) are already implemented as Controls and it would be waste of time to reimplement them using mvc.
If I were you I would try to find some CMS and customize it for WebSite needs.
To responses:
YES. I know about ASP.NET controls disadvantages, but the question is about ASP.NET MVC. One can write a book about what is good and what is bad in ASP.NET but I do not think it is appropriate to discuss it here.
There are better ways of implementing MVC without using asp.net MVC. I have done it in the past, even before asp.net MVC came live. MVC is a pattern, not a technology, I do not understand why some people call it a Technology. You can separate all concerns by removing the code-behind from webforms and create your own controllers and routers and you will still have the advantage of the webform controls, etc to which most asp.net developers are used to use. asp.net mvc is nice for people whom do not really have the time to properly create an MVC app in a webforms environment and also to those whom do not have the time to architect a better solution. in conlcusion, asp.net mvc is good but there is a much better way of doing it and finally, MVC is NOT a technology.

Resources