Routing (routing) –url
URL as a widely used Web user interface that needs to be valued
A good URL should meet the following criteria:
- The URL should provide information for getting some kind of resource, not necessarily the physical file path
- Short and easy to remember and spell input
- Can reflect the site structure
- Should be "detachable", the user removes the end to get a higher level of information
- Lasting and should not change
Which of the following two kinds of URLs do you prefer?
Http://www.cnblogs.com/baka_no/1.html
Http://www.cnblogs.com/baka_no/1
If it is the latter, how does the server recognize it?
ASP. NET MVC uses a routing mechanism to complete the mapping process from URL to specific calling method
Note: Traditionally, URLs represent physical files on a server disk
The role of Routing (routing)
Match incoming requests (do not match server physical files) and map requests to "controller" specific Actions "action method" and "parameters"
Invoke and execute the action method of the corresponding controller class
The identification rules for routing are defined in the Global.asax.cs file
Routes.maproute ("Default",//1. Route name "{Controller}/{action}/{id}",//2. URLs with Parameters New{Controller="Home", Action="Index", id=Urlparameter.optional}//3. Parameter Default value);
Note:{controller} and {action} are specific parameter names and cannot be changed
Requestcontext.routevaluedictionary
SEO: Search Engine Optimization
Routing (route) contains the literal value of the URL
The route URL also allows the inclusion of "literals" in the segment, such as:
/baka_no/{controller}/{action}/{id}
Specifies that the first segment must start with Jiekzou in order to match the route, such as:
/baka_no/home/index/1
URLs can be mixed with literals and parameters in a single period, such as:
Note: You cannot have two consecutive URL parameters: {controller}{action}-{id}
Routing constraints
Allow URL segments to use regular expressions to limit whether a route matches a request
routes. MapRoute ("Blog",//Route name "{Controller}/{action}/{id}",//URL with Parameters New{controller ="Home", action ="Index", id = urlparameter.optional},//parameter Default value New{controller=@"\d{4}", action=@"\d{2}"});
The route matches the incoming URL in a sequential order until the match succeeds to the primary
Named routes
Generates a URL hyperlink for the specified route name
@Html. RouteLink ("Test", "Default",
New {contorller= "home", action = "index", id=1});
Note: Hyperlinks are generated according to the routing rules found (no default values are used)
Action returns different values
viewresult– executes and reads the specified full view
partialviewresult– specifies and reads the specified partial view
Partial view: Mainly provides some HTML code for a page
Typically used in AJAX requests ()
Jsonresult-actionresult– when requesting JSON data
Return JSON (object);//Generate JSON string
string– directly returns a string
Verification Code
Public ActionResult vcodeimg () { new vcode (); byte [] bytes = vcode. Createvimg (); return @" Image/jpeg " );}
Gets the current request path: HtmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
Area areas
MVC project directory Structure Disadvantages:
1. Not conducive to sub-functional collaborative development (shopping cart/commodity management/user Rights Management ...)
2. Code structure is bloated ...
Note: You can use area areas to resolve and even physically separate
Area areas – add
Area Show-In the same project
Area contains:
A new set of MVC folders
**arearegistration.cs file
Area show-in different projects
Zone route Registration Class
Public classwebviewsarearegistration:arearegistration{ Public Override stringAreaName {Get{return "webviews"; } } Public Override voidRegisterarea (AreaRegistrationContext context) {//Registering zone Routescontext. MapRoute ("Webviews_default", "{Controller}/{action}/{id}", New{controller ="Home", action ="Index", id =urlparameter.optional},New string[] {"CRM. Areacontroller.webviews"}//Specifies that the route looks for the namespace of the controller class ); }}
Demo
1. Create a new MvcApplication1 MVC4 project above the solution
2, the project right-click Add->areas, enter "Admin"
3. Add the controller home and add an index view
4. Create a new MvcApplication2 MVC4 project above the solution and delete the Global.asax and Web. config two files
5. Create a new Adminarearegistration class in the root directory, and enter the following:
Public classadminarearegistration:arearegistration { Public Override stringAreaName {Get { return "Admin"; } } Public Override voidRegisterarea (AreaRegistrationContext context) {context. MapRoute ("Admin_default", "Admin/{controller}/{action}/{id}", New{Action="Index", id=urlparameter.optional}); } }
6. Delete the AdminAreaRegistration.cs file under the MvcApplication1 Project/areas/admin folder and the Controllers folder (including HomeController)
7. Create a new HomeController in the controllers of the MvcApplication2 project
8. Remember to keep the mvcapplication1/areas/admin below and refer to the MvcApplication2 project within the MvcApplication1 project
In order for us to automatically sync to the Mvcapplication1/areas/admin/views folder in the MvcApplication2 auto-generated view, you can use the "build event" in the "post-build Event ", open the MvcApplication2 property, and modify the following as follows:
mkdir
"$(SolutionDir)$(SolutionName)\Areas\Admin\Views"
xcopy
"$(ProjectDir)Views"
"$(SolutionDir)$(SolutionName)\Areas\Admin\Views"
/S
/E
/C
/Y
Now you can get the correct results by visiting/admin/home/index again, and you can see that index.cshtml has been copied into the Mvcapplication1/areas/admin/views/home directory.
Area Operation principle
ASP. NET mvc--mvc-Routing