ASP.NET MVC路由扩展:链接和URL的生成(6)

来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:48 点击:

   2: {
   3:     protected void Page_Load(object sender, EventArgs e)
   4:     {
   5:         HttpRequest request = new HttpRequest("default.aspx", "http://localhost:3721/home/index/002", null);
   6:         HttpResponse response = new HttpResponse(new StringWriter());
   7:         HttpContext context = new HttpContext(request, response);
   8:         HttpContextBase contextWrapper = new HttpContextWrapper(context);
   9: 
  10:         RouteData routeData = new RouteData();
  11:         routeData.Values.Add("controller", "home");
  12:         routeData.Values.Add("action", "index");
  13:         routeData.Values.Add("id", "002");
  14:         RequestContext requestContext = new RequestContext(contextWrapper, routeData);
  15:         RouteHelper helper = new RouteHelper(requestContext);
  16: 
  17:         Response.Write(helper.Action("GetProduct", "Products",new {id="002"}) + "<br/>");
  18:         Response.Write(helper.Action("GetProduct", "Products", new { id = "002" }, "http") + "<br/>");
  19:         Response.Write(helper.Action("GetProduct", "Products", new RouteValueDictionary { { "id", "002" } },
  20:             "https", "www.artech.com") + "<br/>");
  21:     }
  22: }
我们通过调用RouteHelper其中其中三个Action方法重载生成出三个Url并写入HTTP回复。对于第一个方法调用,我们指定了Action和Controller的名称以及针对变量{id}的值;第二次在这基础上显示指定了传输协议名称http;第三个在同时指定了协议名称(https)和主机名称(www.artech.com)。当我们通过浏览器访问该Web页面的时候,我们会得到如下图所示3个URL。

 clip_image002

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • 浅析.NET下XML数据访问新机制
  • asp.net 面试+笔试题目第1/2页
  • C# 邮件地址是否合法的验证
  • asp.net 设置GridView的选中行的实现代码
  • C#高级编程:数据库连接[1]
  • 经典C++程序1
  • IIS 自动回收导致后台定时器失效的问题解决
  • ASP.NET&#160;GridView列表代码示例
  • Asp.net MVC源码分析--Action Filter的链式调用
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1