I started learning asp.net mvc on the first day of today. I don't want to write very well. I hope you can study it together.
Now, start learning.
Create a project and select the mvc 4 Application
Next, select basic. Of course, select Razor for the view engine. If we select aspx, we do not need to use mvc.
Here, we will briefly describe that if the project template is left blank, We need to import many files by ourselves, and selecting basic vs will automatically import the required files, people who are not familiar with mvc, be honest first.
The next two Internet and Intranet are web and Intranet. I have never used it, and I am not very familiar with it. Mobile options should be related to mobile phone development, and APIs are provided for third-party interfaces (similar to web Services, lightweight ).
Click OK. vs will import many files to us. Curious, you can take a look at the packages folder under the project folder, which contains many files, including the ef we have learned and the most important mvc library we will learn.
Let's take a look at solution Resource Manager.
App_Data is not very useful, mainly because you can put the database into it, and the contents in this folder are not allowed to be accessed. Deletable
App_Start is some configuration information files used when the website is started. There is a routing file which is relatively important. The content is called in sequence in the Global File.
Briefly analyze the route configuration and check the content in RouteConfig. cs.
1 public static void RegisterRoutes (RouteCollection routes) 2 {3 routes. ignoreRoute ("{resource }. axd/{* pathInfo} "); 4 5 routes. mapRoute (6 name: "Default", 7 url: "{controller}/{action}/{id}", 8 defaults: new {controller = "Home ", action = "Index", id = UrlParameter. optional} 9); 10}View Code
Below are my requests
Because I did not write the full path, the default path is requested. Why is an error reported? That's because I have not written the default home controller and index method. Now let's write down the controller and method.
Add the Controller Home in the Controlllers folder. Note that the Controller name must end with the Controller name.
Below is the code that vs automatically generates. Unfortunately, vs only generates the default index method for us, but does not generate a view for us. The Controller must inherit from the Controller.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. mvc; 6 7 namespace MvcApplication1.Controllers 8 {9 public class HomeController: Controller10 {11 // 12 // GET:/Home/13 14 public ActionResult Index () 15 {16 return View (); 17} 18 19} 20}View Code
Right-click the index method to add a view. But I will not do this first. I will rewrite this method to simplify learning first. Learn about the view later. The following is my method. How is it easy.
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. web; 5 using System. web. mvc; 6 7 namespace MvcApplication1.Controllers 8 {9 public class HomeController: Controller10 {11 // 12 // GET:/Home/13 14 public string Index () 15 {16 return "The first mvc"; 17} 18 19} 20}View Code
Now let's try again the address that reported the error to access the following. How is it? The result is displayed!
Of course, this address is configured by default. The standard address is localhost: 4348/home/index. Well, haha, we finally get rid of aspx, a hateful tail.
In fact, mvc will match the configured route according to the url path. The localhost: 4348/home/index address will be split, the home matches the controller, and the index matching method. We may try it. If you change HomeController, an error will be reported during access. You can also change the controller and action in url: "{controller}/{action}/{id}" in route configuration to url: "{action}/{controller}/{id}", an error occurred while accessing the service? Because the Home address will be used as the method index as the controller when the route resolution address is used, an error will certainly be reported, but we can write the url access address as localhost: 4348/index/home, in this way, you can still access it. Do you understand it?
I just transferred it from aspx. It may not be easy to understand here. Draw a picture for a brief understanding
In the above method, I return a string, which is like a general processing program in aspx. All html code must be written on its own to return to the browser, the view to be learned later (under the Views folder in the project) is the html code. Let's take a look at the view. We add a test method to the home controller.
1 public ViewResult test()2 {3 return View();4 }
You can add a view manually or automatically. First, we will introduce the manual method. Since the test method is in the home controller, add a home folder in the views folder, the method view in all controllers must be under the corresponding controller name file in the views folder. Add a view under Views/Home/. Note that the view name conventions must be the same as the method name. The following describes how to make the method different from the view name.
The following is the view file automatically generated by.
You can also choose to automatically generate a view, that is, right-click the method name to add a view. You can also generate a view file with the same effect.
1 @ {2 Layout = null; 3} 4 5 <! DOCTYPE html> 6 7
The generated file name is the cshtml suffix. Modify the content and access it.
@ {Layout = null ;}<! DOCTYPE html>
Now, let's take a look at the syntax of the razor view engine.