AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo

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" }
          namespaces: new[] { "MvcRouteTest.Controllers" }
      );


          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>

注:上述的的3310 不是固定的,是本地实际运行端口号,根据自己的端口号,修改。

ForbieDownloadRar.cshtml:

@{
    ViewBag.Title = "ForbidDownloadRar";
}

<h2>禁止下载Rar文件</h2>

F.检查 Web.config 中的如下配置是否一样。如果没有请添加,如果不一样,请修改:

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

G.在Global.asax.cs文件中的Application_Start事件中最前面添加一行代码:

 RouteTable.Routes.RouteExistingFiles =true;

通过上述代码true或false的修改,来测试Index主页的各种链接功能.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值