毕业设计外文文献—创建控制器.docxVIP

  • 2
  • 0
  • 约3.64万字
  • 约 27页
  • 2022-09-12 发布于江苏
  • 举报
附录A 外文翻译—原文部分 文章来源:Nate Barbettini The Little ASP.NET Core Book[Z] ADDIN CNKISM.UserStyle Create a controller There are already a few controllers in the projects Controllers directory, including the HomeController that renders the default welcome screen you see when you visit http://localhost:5000. You can ignore these controllers for now. Create a new controller for the to-do list functionality, called TodoController, and add the following code: Controllers/TodoController.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; namespace AspNetCoreTodo.Controllers { public class TodoController : Controller { // Actions go here } } Routes that are handled by controllers are called actions, and are represented by methods in the controller class. For example, the HomeController includes three action methods (Index, About, and Contact) which are mapped by ASP.NET Core to these route URLs: localhost:5000/Home - Index() localhost:5000/Home/About - About() localhost:5000/Home/Contact - Contact() There are a number of conventions (common patterns) used by ASP.NET Core, such as the pattern that FooController becomes /Foo, and the Index action name can be left out of the URL. You can customize this behavior if youd like, but for now, well stick to the default conventions. Add a new action called Index to the TodoController, replacing the // Actions go here comment: public class TodoController : Controller { public IActionResult Index() { // Get to-do items from database // Put items into a model // Render view using the model } } Action methods can return views, JSON data, or HTTP status codes like 200 OK and 404 Not Found. The IActionResult return type gives you the flexibility to return any of these from the action. Its a best practice to keep controllers as lightweight as possible. In this case, the controller will be responsible for

文档评论(0)

1亿VIP精品文档

相关文档