ASP.NET MVC Controller Overview摘錄

來源:互聯網
上載者:User

原文URL:http://www.asp.net/learn/mvc/tutorial-03-cs.aspx

Understanding Controllers

1. Controller定義
  MVC controllers are responsible for responding to requests made against an ASP.NET MVC website. Each browser request is mapped to a particular controller. For example, imagine that you enter the following URL into the address bar of your browser:

  http://localhost/Product/Index/3

  In this case, a controller named ProductController is invoked. The ProductController is responsible for generating the response to the browser request. For example, the controller might return a particular view back to the browser or the controller might redirect the user to another controller.

  A controller is a class that derives from the base System.Web.Mvc.Controller class.

Understanding Controller Actions

1. Action定義
  An action is a method on a controller that gets called when you enter a particular URL in your browser address bar. For example, imagine that you make a request for the following URL:

  http://localhost/Product/Index/3

  In this case, the Index() method is called on the ProductController class. The Index() method is an example of a controller action.

2. Action的使用注意事項
  2.1 A controller action must be a public method of a controller class.
  2.2 A method used as a controller action cannot be overloaded.
  2.3 A controller action cannot be a static method.

Understanding Action Results

1. Action Result定義
  A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

2. Action Results類型
  The ASP.NET MVC framework supports several types of action results including:

  1.ViewResult – Represents HTML and markup.
  2.EmptyResult – Represents no result.
  3.RedirectResult – Represents a redirection to a new URL.
  4.JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
  5.JavaScriptResult – Represents a JavaScript script.
  6.ContentResult – Represents a text result.
  7.FileContentResult – Represents a downloadable file (with the binary content).
  8.FilePathResult – Represents a downloadable file (with a path).
  9.FileStreamResult – Represents a downloadable file (with a file stream).

  All of these action results inherit from the base ActionResult class.

2. Action Results類型使用說明 

  In most cases, a controller action returns a ViewResult.

  Normally, you do not return an action result directly. Instead, you call one of the following methods of the Controller base class:

  1.View – Returns a ViewResult action result.
  2.Redirect – Returns a RedirectResult action result.
  3.RedirectToAction – Returns a RedirectToRouteResult action result.
  4.RedirectToRoute – Returns a RedirectToRouteResult action result.
  5.Json – Returns a JsonResult action result.
  6.JavaScriptResult – Returns a JavaScriptResult.
  7.Content – Returns a ContentResult action result.
  8.File – Returns a FileContentResult, FilePathResult, or FileStreamResult depending on the parameters passed to the method.

  So, if you want to return a View to the browser, you call the View() method. If you want to redirect the user from one controller action to another, you call the RedirectToAction() method.

  The ContentResult action result is special. You can use the ContentResult action result to return an action result as plain text.

  If a controller action returns a result that is not an action result – for example, a date or an integer – then the result is wrapped in a ContentResult automatically.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.