AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo
2015-05-29 09:52
561 查看
A.创建一个Basic类型的MVC项目.
B.在Content文件目录下创建下载文件资源:cs.rar,cs.doc,cs.txt等,见下图右方:
C.修改RouteConfig.cs文件:
D.创建HomeController.cs文件:
E.创建相应的View:
Index.cshtml:
ForbieDownloadRar.cshtml:
F.在Global.asax.cs文件中的Application_Start事件中最前面添加一行代码:
RouteTable.Routes.RouteExistingFiles =true;
(也可参阅上图)
G.通过上述代码true或false的修改,来测试Index主页的各种链接功能.
B.在Content文件目录下创建下载文件资源:cs.rar,cs.doc,cs.txt等,见下图右方:
C.修改RouteConfig.cs文件:
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(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "ForbidDownloadRar", url: "Content/Download/a/b/c/{id}.rar", defaults: new { controller = "Home", action = "ForbidDownloadRar" } ); routes.MapRoute( name: "ForgeHtml", url: "china/htzd/{id}.html", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, ///////////////////路由值得添加约束 constraints: new {id=@"\d+" } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } } }
D.创建HomeController.cs文件:
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 ="传过来的ID为"+ ID.ToString(); } else { ViewBag.str ="传过来的ID为Null"; } return View(); } public ActionResult GetFile() { return File(Server.MapPath("~/Content/download/a/b/c/cs.rar"), "application/x-rar-compressed","cs.rar"); } } }
E.创建相应的View:
Index.cshtml:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <h2>@ViewBag.str</h2> <h2><a href="@Url.Content("~/Content/Download/a/b/c/cs.rar")">Rar下载测试1-使用Url帮助函数</a></h2> <h2><a href="http://localhost:3310/Content/Download/a/b/c/cs.rar">Rar下载测试2-使用链接地址</a></h2> <h2>@Html.ActionLink("Rar下载测试3-使用GetFile.", "GetFile", "Home")</h2> <hr /> <h2><a href="@Url.Content("~/Content/cs.txt")">下载cs.txt测试--使用Url帮助函数</a></h2> <h2><a href="http://localhost:3310/Content/cs.txt")">下载cs.txt测试--使用链接地址</a></h2> <hr /> <h2><a href="@Url.Content("~/Content/cs.doc")">下载cs.doc测试--使用Url帮助函数</a></h2> <h2><a href="http://localhost:3310/Content/cs.doc")">下载cs.doc测试--使用链接地址</a></h2> <hr /> <h2><a href="@Url.Content("~/china/htzd/8.html")">伪造静态页面--使用Url帮助函数</a></h2> <h2><a href="http://localhost:3310/china/htzd/8.html">伪造静态页面--使用链接地址</a></h2>
ForbieDownloadRar.cshtml:
@{ ViewBag.Title = "ForbidDownloadRar"; } <h2>禁止下载Rar文件</h2>
F.在Global.asax.cs文件中的Application_Start事件中最前面添加一行代码:
RouteTable.Routes.RouteExistingFiles =true;
(也可参阅上图)
G.通过上述代码true或false的修改,来测试Index主页的各种链接功能.
相关文章推荐
- c#使用aspose.cells 从datatable导出数据到excel
- c#使用aspose.cells 从datatable导出数据到excel
- 解读ASP.NET 5 & MVC6系列(14):View Component
- ABP(现代ASP.NET样板开发框架)系列之4、ABP模块系统
- ASP.NET菜鸟之路之Response小例子
- 采集/自动登录啊都可以用这两个方法实现 asp.net
- asp.net图片上传步骤
- asp.net Identity2 角色(Role)的使用(三)用户管理,用户控制器和视图
- asp.net Identity2 角色(Role)的使用(二)角色管理,角色控制器和视图
- asp.net 验证正则表达式
- asp.net Identity2 角色(Role)的使用(一)初始配置
- RaspberryPi上检测FreeSWITCH状态及启动FreeSWITCH
- 检测到在集成的托管管道模式下不适用的ASP.NET设置的解决方法
- Asp.Net页面生命周期
- asp.net mvc中DropDownList
- ASP.NET 图片上传工具类 upload image简单好用功能齐全
- 我对 aspnetpager和repeater以及查询条件的封装
- raspbian 更换新源的方法
- [Asp.net 5] Localization-简单易用的本地化-全球化信息
- 自己动手写 ASP.NET MVC 分页 part1