Now let's implement the homepage, the first page in the announcement system. It is very simple. It only includes a list of all announcement categories, and each list item is a hyperlink. The classified data is obtained using our mock component. After implementation, the interface is as follows:
Before we start, we need to delete a few items. By default, a MVC project contains several sample pages. What we need to do is:
1. Delete all files in the controllers folder.
2. Delete all files in the Views folder except the shared folder and web. config, and then delete the files in the shared folder.
After completing the preceding steps, you can start to implement the first page.
Implement Controller
Create a new file in the controllers folder, select "MVC controller class" for the type, and name it homecontroller. CS. This is a controller class. The Code is as follows:
Homecontroller. CS:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using MVCDemo.Models; using MVCDemo.Models.Interfaces; using MVCDemo.Models.Entities;namespace MVCDemo.Controllers{ public class HomeController : Controller { public ActionResult Index() { ICategoryService cServ = ServiceBuilder.BuildCategoryService(); ViewData["Categories"] = cServ.GetAll(); return View("Index"); } }}
Intuitively, this class is not very complex. It first inherits the Controller class. The Controller class is a controller accumulation provided in the ASP. net mvc Framework. All custom controller classes must inherit this base class. There is an index method in this class. The return value type is actionresult.
Here is a brief explanation of the concepts involved. First, the Controller class can be said to be ASP. net MVC core class, because it will process all requests and handle all the page forwarding and other representation logic, which also uses ASP. net MVC and traditional ASP. NET applications. In the traditional mode, the URL requested by a user corresponds to An ASPX file, while in ASP. net MVC, a user request corresponds to a method in a controller class, and this method is called an action. As for how it corresponds, it is through URL parsing.
For example, in traditional cases, http: // localhost/default. aspx indicates requesting the default. aspx file under the root directory of the website. Now, the URL may look like this: http: // localhost/home/index. This means that the request name is the index method under the homecontroller class. Generally, the request URL is in the format of http: // localhost/{controllername}/{actionname} by default }. {Controllername} is the part before the Controller class name "controller", and {actionname} is the method name.
Of course, this ing rule can be changed, and parameters can also be passed when an action is requested. However, these are post-discussions and will be discussed later.
Next we will go deep into the index method to see what this action has done. It first calls the business logic component (of course, mock), then assigns the announcement category data returned by getall to viewdata ["category"], and finally calls the view () method to return an actionresult. What is viewdata? You can regard it as an associated array, which saves the data to be passed to the view. View is a method of the controller class. It returns an actionresult instance. This may be a bit abstract. In fact, it is intuitive to present a view (usually An ASPX file) to a browser. So how can we know which view to present? By default, the view method searches for a view with the same name as the Action Method in the view folder of the website with the same name as the controller class. For example, the index method of homecontroller looks for views/home/index. aspx. If it cannot be found, it searches for views/home/index. aspx under shared. If it cannot be found, an error is returned. Of course, you can also pass a string parameter to the view method to indicate the view name.
Implementation View
As mentioned above, when http: // localhost/home/index is requested, the index method of homecontroller is called, and the index method is finally presented with views/home/index. aspx view, so we need to create a home folder under the views folder, and then create a new index. aspx view. If you are using vs2008 SP1, It is very convenient to create a view. You only need to right-click the home folder, choose add ---> View, and specify the view name. If it is not SP1, create an item and select "MVC view page" for the type ". A created view is actually An ASPX page, but it inherits the view. This is also a base class, And all views need to inherit it.
The index. aspx code is given below:
Index. aspx:
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "index. aspx. CS "inherits =" mvcdemo. views. home. index "%> <% @ import namespace =" mvcdemo. models. entities "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
Analyze it. As mentioned earlier, the index action finally presents the aspx as the view, and viewdata contains the data to be passed to the view. There, we put all the announcement category data in viewdata ["categories. We can see that the data is taken out and used to present the page. The HTML. Action is not detailed here. As long as you know, this method can generate a link. The first parameter is the link text, and the second is the action name of the URL to be linked, the third is the Controller name of the URL to be linked. We will discuss this in detail later.
Run this example and locate the request URL to home/index to see the running effect.
You may find that you do not need to specify the home/index. after entering the root directory, the page is displayed directly. In fact, this is because in the default route configuration, home and index are the default controller name and action name. We will discuss routing issues later.
Summary
Through the above process, the first ASP. net mvc page can be displayed. It is not just a page, but also the data returned by the business logic component.
Maybe you are still confused about many of them. Don't worry. In the next article, we will take a slow step in this system. I will use a whole article to introduce in detail many important concepts and principles in ASP. NET MVC.