AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo
来源:互联网 发布:js 鼠标轨迹特效 编辑:程序博客网 时间:2024/04/30 20:46
A.创建一个Basic类型的MVC项目.
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主页的各种链接功能.
0 0
- AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo
- AspNet MVC4 教学-19:Asp.Net MVC4 利用Linq技术的搜索应用快速Demo
- AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术快速应用Demo
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术快速应用Demo
- AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo
- AspNet MVC4 教学-12:Asp.Net MVC4 Session等技术快速Demo
- AspNet MVC4 教学-13:Asp.Net MVC4 过滤器等技术快速Demo
- AspNet MVC4 教学-14:Asp.Net MVC4 ViewBag等数据传输技术快速比较Demo
- AspNet MVC4 教学-15:Asp.Net MVC4 TempData数据传输技术快速Demo
- AspNet MVC4 教学-21:Asp.Net MVC4 使用Ajax技术获取服务器时间快速Demo
- AspNet MVC4 教学-24:Asp.Net MVC4 Url及Html的Encode技术快速Demo
- AspNet MVC4 教学-25:Asp.Net MVC4 强弱类型View等技术快速对比Demo
- AspNet MVC4 教学-28:Asp.Net MVC4 Ajax技术实现除法取整取余快速Demo
- AspNet MVC4 教学-29:Asp.Net MVC4 jQuery计时器读取服务器时间的Ajax技术快速Demo
- AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo
- AspNet MVC4 教学-11:Asp.Net MVC4 默认Authorize及自定义Authorize快速Demo
- AspNet MVC4 教学-16:Asp.Net MVC4 Session及Cookie快速比较Demo
- AspNet MVC4 教学-17:Asp.Net MVC4 全局对象Application快速Demo
- linux上设置oracle默认的编辑器
- Hadoop报错 " Message missing required fields: callId, status"解决方案
- eclipse4.4的google protocol buffer的proto文件编辑器Protocol Buffer Editor安装
- 【SQL优化】使用子查询可提升 COUNT DISTINCT
- Android Application浅析
- AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo
- sql 查询所有数据库、表名、表字段总结
- Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式
- 会声会影11提示 “保护信息无效或已被修改,程序将中止”解决办法
- WSN 硬件实验平台综述——调研报告
- 河南小儿自闭症yiyuan
- 黑马程序员- java中的排序
- 更改make版本由于编译4.0Android需要3.81make所以重新安装
- Dui界面布局无法响应鼠标点击消息响应