Net5 razor pages vs mvc - asp.net-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.

Related

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

Relation of webforms in 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.

Architectural advice for a SPA ASP.NET application

I'm starting a new project, based on ASP.NET. The application will be a registry with a web interface. It will be used by ca 3000 users.
It has been ca 4 years since I last started a new project so I'm a "bit" rusty when it comes to the latest trends..
I wish the new application to be SPA so I investigated some Javascript MV* frameworks and finally settled on Durandal, Bootstrap (for layout) and Telerik's Kendo UI with ASP.NET MVC helpers.
I installed a template called Hot Towel, which created a ASP.NET MVC based Durandal project setup. However, I see that Views are html not Razor's cshtml. Can I still use cshtml files (for helper classes to work)? Is there any downfall in this?
Also I heared a discussion that when using Durandal, one shouldn't use ASP.NET MVC but rather just Web API. What do you think of that? I'd still like to use MVC helpers here and there..or is there a good reason for abandoning it for Web API?
Do you have any other considerations and recommendations on a new project setup?
Best wishes,
Andrew
Someone who knows a more about durandal than I may be able to shed some light on the part of using cshtml views. I would imagine that you would have to override the viewLocator and point it to controller actions rendering partial views??? (you need them to be parsed by the Razor engine to get valid HTML).
As for using regular html files, I have used a fantastic library called knockout-kendo
for using kendo UI components in SPA applications. I find it just as easy to use as the HTML helpers, all while keeping the application a true SPA, and you then serve up all of your data via WebAPI controllers.Hope this is of some help.
EDIT: Perhaps you can also have a look at this answer How can I use cshtml files with Durandal?. There are a few answers there that seem to be doing what you are looking to do..

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.

Resources