如何在WebForm中使用Url Routing之说明
来源:互联网 发布:landesk软件购买 编辑:程序博客网 时间:2024/05/16 12:54
什么是URL Routing?
所谓URL Routing(URL路由),指的是在Web中,URL指向的不再是某个物理文件,而是一个说明有关URL路由的字符串,开发者可以自定义该字符串的格式。在默认情况下,URL Routing在ASP.NET应用程序中是可以直接使用的,但在ASP.NET站点上,需要做一些配置才能使用
为什么要使用URL Routing?
在使用URL Routing前,我们的URL可能是http://www.website.com/Index.aspx?id=1,使用URL Routing后,我们的URL可以变成http://www.website.com/Index/1。修改后的URL更加友好,更有利于SEO。
URL Routing只能在MVC中才能使用吗?
路由程序集(System.Web.Routing.dll)在.NET Framework V3.5 sp1中就包含了,而MVC是在之后才发布的。因此不能说URL Routing只能在MVC中才能使用。不过在MVC中增加了Routing在一些扩展方法(包含在System.Web.Mvc的RouteCollectionExtemsion类中),使用起来更加方便.
下面简单介绍下如何在Web Form中使用URL Routing。
1. 添加对程序集System.Web.Abstractions.dll,System.Web.Routing.dll的引用.
2. 添加一个IRouteHandler的实现类WebFormRouteHandler
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
/// <summary>
///WebFormRouteHandler 的摘要说明
/// </summary>
public class WebFormRouteHandler:IRouteHandler
{
/// <summary>
/// 具体呈现的页面路径
/// </summary>
public string VirtualPath
{
get;
private set;
}
public WebFormRouteHandler(string virtualpath)
{
this.VirtualPath = virtualpath;
}
#region IRouteHandler 成员
/// <summary>
/// 实现 IHttpHandler接口中的GetHttpHandler方法,
/// </summary>
/// <param name="requestContext"></param>
/// <returns></returns>
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// 从ReqestContext对象中获取URL参数,并把值写到HttpContext的Items集合中供路由目标页面使用
foreach (var urlParm in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;
}
//得到实例化页面对象
var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
#endregion
}
3. 修改配置web.config文件(必要的支持url Routing方法)
<httpModules>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<javascript runat="server">
public static void RegisterRoutes(RouteCollection routes)
{
//注意此处调用的是WebFormRouteHandler类
routes.Add("Named", new Route("NameIndex/{action}", new RouteValueDictionary { { "action", "NameIndex" } }, new WebFormRouteHandler("~/Named/Index.aspx")));
routes.Add("Numbers", new Route("NumberIndex/{action}", new RouteValueDictionary { { "action", "NumberIndex" } }, new WebFormRouteHandler("~/Numbers/Index.aspx")));
//添加验证,要求action参数值必须是13个字符,若action参数长度不等于13个字符(酌情更改),则会得到“无法找到资源”的错误提示。
routes.Add("Validate",new Route("ValidateIndex/{action}",new RouteValueDictionary { { "action", "ValidateIndex" }},new RouteValueDictionary { { "action", @"\w{13}" } },new WebFormRouteHandler("~/Validate/Index.aspx")));
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
</script>
1、Named(文件夹)
下面包含一个文件:Index.aspx
2、Numbers(文件夹)
下面包含一个文件:Index.aspx
3、Validate(文件夹)
下面包含一个文件:Index.aspx
现在要将项目的根目录下的Default.aspx文件稍作修改
添加如下内容:
<div><a href="NameIndex">Go To Named/Index</a></div><br />
<div><a href="NumberIndex">Go To Numbers/Index</a></div><br />
<div><a href="ValidateIndex">Go To ValidateIndex!</a></div>
所有的代码都写完了,下面进行调试,如果没有问题,代表程序可以跑 :)
最后运行程序,点击Default.aspx页面中的三个链接,看看效果 :)
- 如何在WebForm中使用Url Routing之说明
- 在Webform中使用URL路由
- ASP.NET4 WebForm URL Routing
- 如何在WebForm中使用javascript防止连打(双击)
- 在Asp.net WebForm中使用Route
- 如何在webform中为button加入热键?
- WebForm中使用MVC
- 在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- 怎么在ASP.NET WebForm中使用Razor视图引擎
- 在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- 【转】在 ASP.NET MVC 项目中使用 WebForm
- 在 ASP.NET MVC 项目中使用 WebForm
- webform中也可以直接url路由
- MySQL服务器安装完之后如何调节性能
- 如何在Windows下搭建Android开发环境
- VS2008解决“MSVCRT”与其他库的使用冲突的警告
- 大话设计模式之职责链模式
- hdu 1997 汉诺塔VII
- 如何在WebForm中使用Url Routing之说明
- Android开发视频教学第一季(1-16集)视频&源码下载
- 转载_fread的返回值注意事项
- sprintf函数用处详解
- linux 内核编译出现:cc1: error: invalid option `abi=aapcs-linux'
- ios 小技巧(一)
- 多对一 lazy=true 生成的是代理类,属性为null
- String类
- 凹凸贴图(Bump Map)实现原理以及与法线贴图(Normal Map)的区别