標籤:string 協議 ref ring views route ted 虛擬目錄 ace
一、擷取URL(ASP.NET通用):
【1】擷取完整url(協議名+網域名稱+虛擬目錄名+檔案名稱+參數)
string url=Request.Url.ToString();
【2】擷取虛擬目錄名+頁面名+參數:
string url=Request.RawUrl;(或 string url=Request.Url.PathAndQuery;)
【3】擷取虛擬目錄名+頁面名:
string url=HttpContext.Current.Request.Url.AbsolutePath;(或 string url= HttpContext.Current.Request.Path;)
【4】擷取網域名稱:
string url=HttpContext.Current.Request.Url.Host;
【5】擷取參數:
string url= HttpContext.Current.Request.Url.Query;
【6】擷取連接埠:
Request.Url.Port
二、擷取當前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"]
很顯然上面只能擷取當前View的,如果是嵌套的View上面獲得的最終的子View的,如果要擷取父View的,如下
ControllerContext.ParentActionViewContext.RouteData.Values["controller"]ControllerContext.ParentActionViewContext.RouteData.Values["action"]在視圖中@ViewContext.ParentActionViewContext.RouteData.Values["controller"]@CViewContext.ParentActionViewContext.RouteData.Values["action"]
參考:
http://stackoverflow.com/questions/4412310/how-to-get-current-controller-and-action-from-inside-child-action
http://blog.itpub.net/28699126/viewspace-1131655/
asp.net MVC中擷取當前URL/Controller/Action