ASP.NET路由应用及IIS配置(非MVC)

一、前后台代码:

Global.cs:

using System.Web.Routing;

。。。      

void Application_Start(object sender, EventArgs e)

        {

            // 在应用程序启动时运行的代码

            //RegisterRoutes();

            RegisterRoutes(RouteTable.Routes);

        }

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.MapPageRoute("ProductsRoute", "Products/list-{lbid}", "~/Products/ProList.aspx");

            routes.MapPageRoute("ProductsDetail", "Products/show-{id}.html", "~/Products/Show.aspx");

        }

Default.aspx添加链接:

<p>

    <ul>

      <li><a href="#">产品中心</a></li>

      <li>·<a href="Products/list-1">产品分类1</a></li>

      <li>·<a href="Products/list-2">产品分类2</a></li>

    </ul>

</p>

<p>

    <ul>

      <li><a href="Products/show-1.html">产品1</a></li>

      <li>·<a href="Products/show-2.html">产品2</a></li>

      <li>·<a href="Products/show-3.html">产品3</a></li>

    </ul>

</p>

ProList.aspx.cs 中接收路由参数:

protected void Page_Load(object sender, EventArgs e)

        {

            string lbid = Page.RouteData.Values["lbid"] as string;//接收路由参数

            lblLbid.Text = "lbid="+lbid;

            //下面写业务逻辑

        }

Products/Show.aspx 接收路由参数:

protected void Page_Load(object sender, EventArgs e)

        {

            lblMsg.Text = "id=" + Page.RouteData.Values["id"] as string;//接收路由参数;

        }  

 效果如图:

二、IIS部署

如果访问html时出现 404 错误,有以下两种解决方法:

1. 在IIS7.0中,需要把网站应用程序池中的托管管道模式设置为:集成。否则会提示 404 错误。

2. 如果实际条件不允许设置为集成模式,则需要添加“处理应用程序映射”,需添加.html文件,使用asp.net api 处理。

原文地址:https://www.cnblogs.com/guo2001china/p/3736585.html