A. Create a basic type of MVC project.
B. Create a download file resource in the content file directory: Cs.rar,cs.doc,cs.txt, etc., see right:
C. Modify the RouteConfig.cs file:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.routing;namespace mvcroutetest{public class Routeconfig {public static void RegisterRoutes (Route Collection routes) {routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute (Name: "Forbiddownloadrar", url: "Content/download/a/b/c/{id}.rar", defaults:new {Co Ntroller = "Home", action = "Forbiddownloadrar"}); Routes. MapRoute (Name: "forgehtml", url: "china/htzd/{id}.html", defaults:new {controller = "Home", Action = "Index", id = urlparameter.optional},///////////////////route is worth adding constraints constraints:new {[email protected] "\d+"}); Routes. MapRoute (Name: "Default", url: "{controller}/{action}/{id}", defaults:new {controller = " Home ", action =" Index ", id = urlparameter.optional} ); } }}
D. Create the HomeController.cs file:
Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;namespace mvcroutetest.controllers{public class Homecontroller:controller { //// GET:/home/ Public ActionResult Forbiddownloadrar () { return View (); } Public ActionResult Index (int?) ID) { if (ID. HasValue) { VIEWBAG.STR = "Passed ID is" + ID. ToString (); } else { viewbag.str = "pass-through ID is null"; } return View (); } Public ActionResult GetFile () { return File (Server.MapPath ("~/content/download/a/b/c/cs.rar"), " Application/x-rar-compressed "," Cs.rar ");}}}
E. Create the appropriate view:
Index.cshtml:
@{viewbag.title = "Index";}
Forbiedownloadrar.cshtml:@{ viewbag.title = "Forbiddownloadrar";}
F. In the Application_Start event in the Global.asax.cs file, add a line of code to the front:RouteTable.Routes.RouteExistingFiles =true;
(also refer to)
G. Test the various link functions of the index home page by modifying the code true or false above.
AspNet MVC4 teaching -20:asp.net MVC4 Routing Technology Rapid Application demo