Understanding Controllers
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.
Understanding Controller Actions
A controller exposes controller actions. 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.
A controller action must be a public method of a controller class. C# methods, by default, are private methods. Realize that any public method that you add to a controller class is exposed as a controller action automatically (You must be careful about this since a controller action can be invoked by anyone in the universe simply by typing the right URL into a browser address bar).
There are some additional requirements that must be satisfied by a controller action. A method used as a controller action cannot be overloaded. Furthermore, a controller action cannot be a static method. Other than that, you can use just about any method as a controller action.
Understanding Action Results
A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.
ASP.NET MVC framework supports several types of action results including:
- ViewResult – Represents HTML and markup.
- EmptyResult – Represents no result.
- RedirectResult – Represents a redirection to a new URL.
- JsonResult – Represents a JavaScript Object Notation result that can be used in an AJAX application.
- JavaScriptResult – Represents a JavaScript script.
- ContentResult – Represents a text result.
- FileContentResult – Represents a downloadable file (with the binary content).
- FilePathResult – Represents a downloadable file (with a path).
- FileStreamResult – Represents a downloadable file (with a file stream).
Understanding Views
ASP.NET or Active Server Pages, ASP.NET MVC does not include anything that directly corresponds to a page. In an ASP.NET MVC application, there is not a page on disk that corresponds to the path in the URL that you type into the address bar of your browser. The closest thing to a page in an ASP.NET MVC application is something called a view.
ASP.NET MVC application, incoming browser requests are mapped to controller actions. A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.
Listing 1 contains a simple controller named the HomeController. The HomeController exposes two controller actions named Index() and Details().
The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):
- Html.ActionLink()
- Html.BeginForm()
- Html.CheckBox()
- Html.DropDownList()
- Html.EndForm()
- Html.Hidden()
- Html.ListBox()
- Html.Password()
- Html.RadioButton()
- Html.TextArea()
- Html.TextBox()
The Different Types of Filters
The ASP.NET MVC framework supports four different types of filters:
- Authorization filters – Implements the
IAuthorizationFilter
attribute. - Action filters – Implements the
IActionFilter
attribute. - Result filters – Implements the
IResultFilter
attribute. - Exception filters – Implements the
IExceptionFilter
attribute.
Filters are executed in the order listed above. For example, authorization filters are always executed before action filters and exception filters are always executed after every other type of filter.
Authorization filters are used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns.
Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.
Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.
Each different type of filter is executed in a particular order. If you want to control the order in which filters of the same type are executed then you can set a filter's Order property.
Improving Performance with Output Caching (C#)
The goal of this tutorial is to explain how you can dramatically improve the performance of an ASP.NET MVC application by taking advantage of the output cache. The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked.
Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index. Normally, each and every time that a user invokes the controller action that returns the Index view, the set of database records must be retrieved from the database by executing a database query.
If, on the other hand, you take advantage of the output cache then you can avoid executing a database query every time any user invokes the same controller action. The view can be retrieved from the cache instead of being regenerated from the controller action. Caching enables you to avoid performing redundant work on the server.
Where Content is Cached
By default, when you use the [OutputCache] attribute, content is cached in three locations: the web server, any proxy servers, and the web browser. You can control exactly where content is cached by modifying the Location property of the [OutputCache] attribute.
You can set the Location property to any one of the following values:
· Any
· Client
· Downstream
· Server
· None
· ServerAndClient
这周算是抽空把mvc 的基本内容都看了一遍, 基本了解asp.net mvc的架构和应用。
后面还是稍微实践一下。 抽空做一个小应用。
最佳实践呀 。