AspNet MVC4 teaching -20:asp.net MVC4 Routing Technology Rapid Application demo

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.