本文實(shí)例講述了ASP.NET實(shí)現(xiàn)MVC中獲取當(dāng)前URL、controller及action的方法。分享給大家供大家參考,具體如下:
URL的獲取很簡(jiǎn)單,ASP.NET通用:
【1】獲取 完整url
(協(xié)議名+域名+虛擬目錄名+文件名+參數(shù))
string url=Request.Url.ToString();
【2】獲取 虛擬目錄名+頁(yè)面名+參數(shù):
string url=Request.RawUrl;
或
string url=Request.Url.PathAndQuery;
【3】獲取
虛擬目錄名+頁(yè)面名:
string url=HttpContext.Current.Request.Url.AbsolutePath;
或:
string url= HttpContext.Current.Request.Path;
【4】獲取 域名:
string url=HttpContext.Current.Request.Url.Host;
【5】獲取 參數(shù):
string url= HttpContext.Current.Request.Url.Query;
【6】獲取 端口:
Request.Url.Port
二、當(dāng)前controller、action的獲取
RouteData.Route.GetRouteData(this.HttpContext).Values["controller"] RouteData.Route.GetRouteData(this.HttpContext).Values["action"]
或:
RouteData.Values["controller"] RouteData.Values["action"]
如果在視圖中可以用:
ViewContext.RouteData.Route.GetRouteData(this.Context).Values["controller"] ViewContext.RouteData.Route.GetRouteData(this.Context).Values["action"]
或:
ViewContext.RouteData.Values["controller"] ViewContext.RouteData.Values["action"]
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《asp.net優(yōu)化技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com