The equivalent to Web Forms in ASP.NET Core?

自作多情 提交于 2019-12-20 12:38:55

问题


Why? Hi, I am doing my final year project in Computer Science and the project is a system for managing a college, like openSIS, but using ASP.NET Core to build it.

What? I need help on how to Add, Update, and Remove data using regular HTML forms, such as via <form/> and <input type="submit"/>.

If this is too-broad a question, it would help to have some guidance on how to narrow the scope of the question to a set of subjects relevant to completing this project.


回答1:


What you're asking to do is possible "out of the box" with ASP.NET Core and does not require a third-party solution.

Solution #1: ASP.NET Core Razor Pages

This is probably the most direct approach with the least to learn, and it is the modern equivalent to "Classic ASP.NET Web Forms." You are probably asking about this:

Microsoft - Introduction to Razor Pages

The basic idea is you create an "ASP.NET Core Web Application" project in Visual Studio and select the "Web Application" project template (confirming that ".NET Core" is selected in the template selection dialog before you click OK.)

You can then add "Razor Pages" containing HTML, CSS, and JavaScript, and use a simple post-back method via a regular HTML Form with a Submit Button. The link above has a topic on doing just that.

Solution #2: ASP.NET Core MVC

This is the second-most direct approach but does require you to grasp MVC concepts: Models, Views and Controllers. Implementing Razor Pages would only require you to grasp HTML and Razor Page code-behind, however, MVC as a pattern will provide your resulting project with a more maintainable and testable approach to the project.

Microsoft - Overview of ASP.NET Core MVC

The basic idea is to scaffold an "ASP.NET Core Web Application" project in Visual Studio, then select the "Web Application (Model-View-Controller)" project template.

Instead of adding Pages you add Views, to do anything useful these Views will have corresponding Controllers. There will also be Models defined, as you would see in a Razor Pages solution. In the case of MVC the Models are posted to your Controllers via Actions.

The link above covers the subject in detail, and there are numerous Stack Overflow posts on general MVC concepts.

Solution #3: RESTful Web APIs and Client-Side JavaScript

This is probably the least direct and most-technical approach. You will want to be familiar with "JavaScript" and "XHRs", "JSON" (or XML, but JSON has become de facto), and you will want to be versed in what it means to be a "RESTful Web Service" ie. that HTTP supports basic verbs to POST, GET, and DELETE a resource.

Microsoft - Building Web APIs

The basic idea is you create an "ASP.NET Core Web Application" project in Visual Studio using the MVC scaffold. You then implement Controllers to behave as RESTful web services. You can add Razor Pages and/or MVC Views to deliver the HTML and JavaScript.

With this approach you can create "Single-Page Applications" that never need to reload, where the HTML and JavaScript for the entire application can be delivered up-front and then RESTful web services used to modify the UI at the client. This can get advanced, and most developers will want to look into using a client-side templating engine. The leanest and simplest to use is probably going to be KnockoutJS, but others are more popular (and more complex.) Knockout has the value of solving a very specific problem: data-binding an HTML UI to JavaScript objects.

Regardless of the approach you take, start here:

Microsoft - Get Started with ASP.NET Core

HTH!




回答2:


If you like the Web Forms way of development, or if you have a large ASP.NET Web Forms application and you need to implement new pages in it, you can try DotVVM.

It is not "Web Forms on .NET Core", but:

  • many concepts in DotVVM are similar to ASP.NET Web Forms (postbacks, server controls, master pages, even the names of the controls and page lifecycle events)
  • it is easy to learn for ASP.NET Web Forms developers
  • no cryptic viewstate hidden field
  • the controls don't produce ugly HTML
  • the MVVM pattern is used
  • no need to know or write JavaScript - C#, HTML and CSS is enough to start coding
  • DotVVM supports both .NET Core and full .NET Framework
  • can be added to existing ASP.NET Web Forms or MVC applications on .NET Framework
  • DotVVM is open source
  • Visual Studio extension with IntelliSense and project templates

Disclaimer: I am one of the authors of DotVVM. This post is not meant to advertise the project, I just believe it responds to the question because the main motivation to build DotVVM was the fact that Web Forms are not ported to ASP.NET Core, and many people is looking for the similar way of building web applications. DotVVM is not a port of Web Forms to .NET Core. Our intent is to build a framework which is conceptually similar, but which avoids the things Web Forms were criticized for (viewstate, testability and ugly HTML output).

  • DotVVM GitHub
  • Documentation
  • Gitter Chat



回答3:


If you are referring to WebForms from ASP.NET (before MVC webstack came), there is no WebForms for ASP.NET Core and unlikely to ever be ported.

There is however a similar project (usually referred as Razor Pages or View Pages, see RazorPages GitHub repository) which allows to create Razor views which are not backed by a controller.

But as far as I know it's not release ready (there is no nuget package for it on nuget.org) and you'd have to use some previous from nightly builds or stable myget repositories.



来源:https://stackoverflow.com/questions/42599341/the-equivalent-to-web-forms-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!