ASP.NET通過(guò)更改Url進(jìn)行頁(yè)面?zhèn)髦档膶?shí)現(xiàn)代碼
這里,通過(guò)假數(shù)據(jù),手動(dòng)創(chuàng)建的一個(gè)類,然后創(chuàng)建的一個(gè)集合,放入下拉框,選好值以后,點(diǎn)確定
會(huì)在另一個(gè)頁(yè)面產(chǎn)生對(duì)應(yīng)的id
創(chuàng)建一個(gè)類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
public class Dept
{
public int Id { get; set; }
public string DeptName { get; set; }
}
}
一個(gè)選擇的web窗體
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Dept.aspx.cs" Inherits="WebApplication1.Dept1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> </asp:DropDownList> </div> <p>><a href="dept_<%=DropDownList1.SelectedValue %>.html" rel="external nofollow" >查詢</a></p> </form> </body> </html>
選擇的web窗體的后臺(tái)代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Dept1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadDeptData();
}
}
private void LoadDeptData()
{
//手動(dòng)創(chuàng)建數(shù)據(jù)
List<Dept> depts = new List<Dept>
{
new Dept{Id=1,DeptName="小明"},
new Dept{Id=2,DeptName="小王"},
new Dept{Id=3,DeptName="小李"}
};
this.DropDownList1.DataSource = depts;
//默認(rèn)顯示的值
this.DropDownList1.DataTextField = "DeptName";
this.DropDownList1.DataValueField = "Id";
//保存
this.DropDownList1.DataBind();
}
}
}
建一個(gè)繼承Modules類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
namespace WebApplication1.Modules
{
public class DeptModule : IHttpModule
{
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
//處理請(qǐng)求
//獲取請(qǐng)求url
HttpApplication application = sender as HttpApplication;
//相對(duì)路徑
string url = application.Request.RawUrl;
//一個(gè)正則,用來(lái)匹配是不是相對(duì)應(yīng)的頁(yè)面
Regex regex = new Regex(@"dept_(\d+).html");
//正則的匹配后的,微軟給鋪好的路,正則匹配后的一個(gè)數(shù)組;
GroupCollection groupCollection = regex.Match(url).Groups;
//這里取得是數(shù)組的第一個(gè)值,看看是不是成功匹配了,
if (groupCollection[0].Success)
{
//取到第二個(gè)值
var id = groupCollection[1].Value.Trim('_');
//存儲(chǔ)id,等用到的時(shí)候直接去第二個(gè)頁(yè)面去取值
HttpContext.Current.RewritePath("~/DeptDetail.aspx","","deptid="+id);
}
}
}
}
建完了類,要進(jìn)入配置文件進(jìn)行配置
因?yàn)槲疫@里是放在一個(gè)文件夾下面了,所以配置文件指定type的時(shí)候,要加一個(gè)文件夾的路徑

<system.webServer> <modules> <add name="Module" type="WebApplication1.Modules.DeptModule"/> </modules> </system.webServer>
顯示的web窗體
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DeptDetail.aspx.cs" Inherits="WebApplication1.DeptDetail" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form> </body> </html>
顯示的web窗體的后臺(tái)代碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class DeptDetail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//直接通過(guò)request獲取Module存入的id
this.TextBox1.Text = $"{Request.QueryString["deptid"]}";
}
}
}
}
效果圖
選擇一個(gè)后點(diǎn)擊查詢

地址欄和內(nèi)容都進(jìn)行了更改

到此這篇關(guān)于ASP.NET通過(guò)更改Url進(jìn)行頁(yè)面?zhèn)髦档奈恼戮徒榻B到這了,更多相關(guān)asp.net url 頁(yè)面?zhèn)髦祪?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
dz asp.net論壇中函數(shù)--根據(jù)Url獲得源文件內(nèi)容
從asp.net dz論壇發(fā)現(xiàn)的這個(gè)函數(shù),學(xué)習(xí)一下高手的經(jīng)驗(yàn)代碼2008-09-09
.net面向?qū)ο笾嗑€程(Multithreading)及 多線程高級(jí)應(yīng)用
這篇文章主要介紹.net面向?qū)ο蟪绦蛟O(shè)計(jì)階段多線程Multithreading及多線程高級(jí)應(yīng)用的介紹,需要的朋友可以參考下2015-07-07
ASP.net 路徑問(wèn)題 詳細(xì)說(shuō)明
能詳細(xì)解釋一下:Request.ApplicationPath,~/,/,./,../及其用法(可時(shí)用)嗎?老是在相對(duì)路徑問(wèn)題上出錯(cuò)。 問(wèn)題點(diǎn)數(shù)2009-06-06
一個(gè).net 壓縮位圖至JPEG的實(shí)例代碼
這篇文章介紹了.net 壓縮位圖至JPEG的實(shí)例代碼,有需要的朋友可以參考一下2013-11-11
ASP.NET Core Web App應(yīng)用第三方Bootstrap模板的方法教程
這篇文章主要給大家介紹了關(guān)于ASP.NET Core Web App應(yīng)用第三方Bootstrap模板的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧2018-06-06
ASP.NET Core 配置和使用環(huán)境變量的實(shí)現(xiàn)
這篇文章主要介紹了ASP.NET Core 配置和使用環(huán)境變量的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn)
這篇文章主要介紹了手把手教你AspNetCore WebApi數(shù)據(jù)驗(yàn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
asp.net簡(jiǎn)單實(shí)現(xiàn)單點(diǎn)登錄(SSO)的方法
這篇文章主要介紹了asp.net簡(jiǎn)單實(shí)現(xiàn)單點(diǎn)登錄(SSO)的方法,結(jié)合簡(jiǎn)單實(shí)例形式較為詳細(xì)的分析了單點(diǎn)登錄的原理與asp.net的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-12-12

