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。
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>