When to use ASP.NET MVC vs. ASP.NET Web Forms?

前端 未结 6 1426
梦毁少年i
梦毁少年i 2020-12-09 09:23

One of the common questions asked regarding ASP.NET MVC is why should you use it over ASP.NET Web Forms? The answer generally includes ViewState and clean URLs, amongst oth

相关标签:
6条回答
  • 2020-12-09 09:35

    I've worked with Web forms for 13 years and MVC for 2 years now and when I started with MVC, I had similar questions. Here are my takeaways.

    • Most importantly: ASP.NET's latest release is 4.6 and they were moving to ASP.NET 5.0, but MS abandoned that for ASP.NET Core, which no longer supports Web Forms (or even VB.NET). So, that alone might give you your answer when deciding what rabbit hole to go down.

    That being said:

    • MVC I'm finding, once you get the hang of it, is WAY easier for dealing with basic forms and any sort of simple "Model", aka tables with a very simple, straight-forward set of relationships such as orders that have tables that link to users, products, etc. Once you start getting into some more complicated relationships and need to return lots of conditional sets of results, rely on parameters, have complicated stored procedures... then Web Forms is much better for dealing with this. If you don't have to deal with this level of complication, MVC makes development SO MUCH faster, especially with dealing with an approach where you already have the DB because it creates so much of the code and validation for you already

    • If you're not very experienced with database design, MVC does the work for you. It can literally build the database for you.

    • MVC doesn't have a lot of the built in controls that Web Forms does (Gridviews, FormViews, Sitemaps, Paged lists). Everything has to be written from scratch, but luckily a lot of people have already invented that stuff for you in NuGet which you can just download into your project

    • MVC relies heavily on the structure of your URL. The path, the querystring, etc. If you find your application needing to do a lot of form POSTing instead of GET-ting, you're going to have to do a lot of tweaking or AJAX posting. If you have a set URL that can't change, it can be a pain. It's doable, but just a little tricker (or you can just use Angular instead).

    • MVC has no Viewstate. If you need to hide variables from post to post and persist them, it's a little difficult. MVC Does have things like ViewBag which lets you pass data from your controller to your page, but it clears after the page is rendered. There is also something called "Tempdata" which acts like Session state, but more temporary. However, it relies on Session State which is not an ideal way of persisting data. Session variables and tempdata variables are fine for user-level data (profile information for the person logged in), but having two different tabs open by the same user can cause these session/tempdata variables to overwrite each other when you're dealing with the actual model data.

    If you're at a crossroads, I'd go with MVC. MS is pushing it and support for Web Forms will likely start going away

    0 讨论(0)
  • 2020-12-09 09:36

    I'll give you a couple purposes, with clear advantages.

    • If your purpose is a public facing website that will be banking on traffic, use MVC. It is optimal for search engine optimization.

    • If your purpose is an enterprise web-application that acts like a desktop app, I would lean towards web forms, since state management and compartmentalization of your resources into underlying server controls offers huge advantages if used correctly.

    0 讨论(0)
  • 2020-12-09 09:36

    The biggest problems facing developers is managing complexity and keeping code "clean". MVC gives the developer the reins to leverage OOP to tuck away complexity and make code easy on the eyes.

    Webforms will be faster to develop in the short term, but it doesn't lend itself to long term sustainability in terms of maintenance and growth.

    0 讨论(0)
  • 2020-12-09 09:49

    You don't choose ASP.Net MVC over ASP.Net, because ASP.Net MVC still is ASP.Net. You do choose ASP.Net MVC or ASP.Net Web Forms, and there are lots of good reasons to do that:

    • Easier to get control over your HTML
    • Easier to do unit testing
    • Few "Gotchas"

    On the other hand, Web Forms do have a few points in their favor:

    • Easy to put simple CRUD/business apps together extremely fast
    • Hard to beat ViewState performance in local LAN environment
    • Easy to learn forms paradigm

    The result is that if you're building business apps in a corporate LAN environment (which honestly is still most web developers), Web Forms is really great. In that sense Microsoft really knows their market. But if you're building an app for the public internet, you might want MVC so you can test thoroughly and be sure your pages aren't bloated with unnecessary ViewState or JavaScript data.

    Additionally, something that has changed over the last several years is that even many corporate intranet applications now need to support home/remote use, making MVC more attractive to that crowd than it had been.

    0 讨论(0)
  • 2020-12-09 09:49

    http://weblogs.asp.net/shijuvarghese/archive/2008/07/09/asp-net-mvc-vs-asp-net-web-form.aspx

    check that blog !

    Bottom line "separation of concerns"

    0 讨论(0)
  • 2020-12-09 09:54

    Use MVC if all your team members are skilled enough to manage "control over HTML", otherwise your code will turn into a tag soup.

    In other words

    bool useMvc = true;
    foreach (TeamMember member in team.Members)
    {
        useMvc = useMvc && member.IsSkilled;
    } 
    
    0 讨论(0)
提交回复
热议问题