Edited, memorised or added to reading queue

on 17-Jan-2016 (Sun)

Do you want BuboFlash to help you learning these things? Click here to log in or create user.

ROUTES AND ROUTING
It is difficult to talk about controllers without including routes. The routing table is stored in the Global.asax file. The routing system enables you to define URL mapping routes and then handle the mapping to the right controller and actions. It also helps construct outgoing URLs used to call back to the controller/actions. ASP.NET provides some default routing. The default routing format is {controller}/{action}/ {id}. That means an HTTP request to http://myurl/Product/Detail/1 will look for the Detail action on the ProductController that accepts an integer as a parameter. The routing engine doesn’t know anything about ASP.NET MVC; its only job is to analyze URLs and pass control to the route handler. The route handler is there to find an HTTP handler, or an object imple- menting the IHttpHandler interface, for a request. MvcHandler, the default handler that comes with ASP.NET MVC, extracts the controller information by comparing the request with the template values in the routing table. The handler extracts the string and sends it to a control- ler factory that returns the appropriate controller. The controller factory is easily extendable by creating a custom controller factory that implements IControllerFactory.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




ASYNCHRONOUS CONTROLLERS One of the major changes in ASP.NET MVC 4 involves asynchronous controllers. ASP.NET MVC 3 uses an AsyncController class that needs to be implemented to have asynchronous control- lers. ASP.NET MVC 4 brings the concept of asynchronous controllers into the default control- ler class. Asynchronous action methods are useful for long-running, non-CPU-bound requests because they avoid blocking the web server from performing work while the method request is still pending. When designing your action methods, you need to determine whether to use synchronous or asynchronous processing. You should strongly consider asynchronous methods when the operation is network-bound or I/O-bound rather than CPU-bound. Also, asynchronous methods make sense when you want to enable the user to cancel a long- running method. Modern computers have processors that have multiple cores, which makes multithreading even more important because it is gaining more support with every computer generation. Being able to do work on multiple threads allows parallel processing, which should result in an increase in performance, especially when multiple long-running processes occur during the same HTTP request. When designing your ASP.NET MVC 4 application, you should look at every process that reaches outside of your domain and consider making them asynchronous. You should do the same for those calls that might be long-running, such as pages that return lists from multiple data sources or that perform intensive business operations, because they could be ideal candidates for the using of asynchronous behavior. Using asynchronous actions is easy with ASP.NET MVC 4. The key to using the new asyn- chronous framework is the Task framework in the System.Threading.Tasks namespace. The purpose of Task is to provide a pluggable architecture to increase flexibility and to make mul- titasking applications easier to write. To create an asynchronous action on a controller, mark the controller as async and change the return from an ActionResult into a Task. In the C# code in Listing 1-1, the application is making a call to an external data feed
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




12 ChAPTER 1 Design the application architecture LISTING 1-1 Calling an external data feed public async Task List() { ViewBag.SyncOrAsync = "Asynchronous"; string results = string.Empty; using (HttpClient httpClient = new HttpClient() { var response = await httpClient.GetAsync(new Uri("http://externalfeedsite")); Byte[] downloadedBytes = await response.Content.ReadAsByteArrayAsync(); Encoding encoding = new ASCIIEncoding(); results = encoding.GetString(downloadedBytes); } return PartialView("partialViewName", results); Asynchronous programming gives you different ways to solve performance issues where multithreading might help. You can create an action that returns synchronously but uses asynchronous work within the method to get work done faster. (The main thread has to wait only for the longest-running work unit to respond rather than waiting for all the work to occur, one after the other.) This kind of approach makes sense if you are merging the results from multiple service calls into a single model to be passed to the view. Another approach is to use an asynchronous partial view, such as in Listing 1-1. This helps the overall performance of your application by running the work in that partial view in a different thread, enabling the primary thread to continue to process other items. It also helps you avoid thread locking because your MVC4 application parses the action. A third approach is to break content out on the page and load it asynchronously from the client. A typical use case is to create your page normally, but rather than directly calling the action result @Html.Partial(“LeadArticleControl”, Model.LeadArticle) in your .cshtml file, you instead use JavaScript code that calls the server to ask for the partial view result after the page has been rendered on the client side, a traditional AJAX approach.
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs




Partial view ASP.NET MVCs version of a user control that can be displayed within a page. The Razor view engine displays it the same as a full view, but without including the and tags. Master or layout page A way to share a design across multiple pages. This page is a building block for the application because it contains much of the wrapper HTML code that turns your output into a format understood by web browsers. Scaffold template A template that creates standard pages as part of the process when creating a project. This ability gives you a quick start on development. Because the default scaffold types are Visual Studio T4 templates, you can alter the existing scaffold types or create a new one. Figure 1-4 shows how the design of a rendered page might have been built when a layout page is used by a view that also contains a partial view. FIGURE 1-4 Rendered page with view relationships
statusnot read reprioritisations
last reprioritisation on suggested re-reading day
started reading on finished reading on

pdf

cannot see any pdfs