I have created several programs using MVC but they all have the same processes in their controller (CRUD), so I wanted to know if something can be done in MVC besides CRUD
Related
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.
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
Is it possible to directly return a Classic ASP web page as an Action Result etc? I have just inherited a stack of Classic ASP applications that are still being maintained and even extended.
I am looking to stop writing new Classic ASP code move to an MVC based setup (as that is the direction the department is going). Therefore I am looking for any options that don't require me to rewrite all the applications but allow me to slowly add features written in ASP.Net.
Ideally I would like to organise features around controllers etc and then return existing ASP pages as the result of Actions. I'd like the URL structure to look the same as a normal MVC site.
Any thoughts or advice? I know there are other posts on this. Haven't seen any that are trying to return ASP pages via Action Result etc.
Thanks
Graeme
Is it possible to directly return a Classic ASP web page as an Action Result etc?
No. The best you could do is redirect. If you are migrating some legacy classic ASP site to ASP.NET MVC you could do it progressively. You can start replacing pages one by one by introducing controllers, models and views. iframes could also be used as a temporary solution to interoperate between the two technologies during the migration.
My company is standardizing on 3 different development models for our client code (we use web services for the business layer code):
ASP.NET MVC
SharePoint 2010
WPF
Our goal is to be able to make changes and new apps quickly via re-use. I have done a lot of research into WPF and SharePoint 2010. WPF has Prism and SharePoint has WebParts (both of these allow reuse of UI components).
But we are planning to send a fair amount of the development to the ASP.NET MVC methodology. Much of this work will have common UI pieces. Since I have not done much research on ASP.NET MVC, I am hoping someone out there can enlighten me.
How is UI reuse done in ASP.NET MVC? (Meaning what is the Prism or WebParts of ASP.NET MVC?)
In MVC you have Partials. Partial controls (ASCX files) that you can render via various means:
RenderPartial(controlname),
RenderAction(action, controller), prepare the data in the controller action and then return the view with this data,
if it's just HTML or JS that you want to reuse and render out, you also have an option of HTML Helpers,
and then with Razor View Engine that comes in MVC 3 there will be new options like Layouts and Sections.
The first part of sharing UI between projects is the controllers, which is easy enough - inherit from a base controller in your shared library, and all the actions in that will work as if they were defined in each project.
You also need views of course, which is a bit harder. You need to either copy the view files into each project or set them up as embedded resources. It takes a bit of work to set up, but it can be done - see Views in separate assemblies in ASP.NET MVC
I am little bit confused by the concept of cms in asp.net mvc application. The question is the following: How can I add controllers and some processing in the view page in the cms?
For instance in cms I create a page called "Account Details". For this logically I need a controller that would receive and process the request. The processing includes validation, db retrieval, etc. In the view part, I need a set of text boxes, etc. As I know, in cms I cannot create something like this. How to proceed in this way?
Best to look at some existing MVC CMS projects and see how they do it. Here is a thread on SO that covers some of the open source MVC CMS's.
Building a CMS in ASP.NET MVC
Check out the answers in this thread: Simple CMS for building a small company presentation-website.