在asp.net WebForms中使用路由Route

1、新建WebForms应用程序

2、打开Global.asax文件代码如下:

public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("",
                "Test/{id}",
                "~/Test.aspx");
        }
    }

3、Test.aspx页面

 前台:

<body>
    <form id="form1" runat="server">
    <div>
        <%=strId%>
    </div>
    </form>
</body>

后台:

namespace WebformRouteTest
{
    public partial class Test : System.Web.UI.Page
    {
        public string strId = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //strId = Request.QueryString["id"];//传统方式
                strId = Page.RouteData.Values["id"] as string;
            }
        }
    }
}

4、运行结果:

输入地址:http://localhost:50649/Test/123,结果如下图:

输入地址 http://localhost:50649/Test.aspx?id=123 一样可以。

本篇文章只是小测试而已,

参考文章:https://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application

原文地址:https://www.cnblogs.com/qk2014/p/5407076.html