• 在ASP.NET MVC 中获取当前URL、controller、action(转载)


    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"]

  • 相关阅读:
    814. Binary Tree Pruning
    50. Pow(x, n)
    698. Partition to K Equal Sum Subsets
    416. Partition Equal Subset Sum
    150. Evaluate Reverse Polish Notation
    322. Coin Change
    Vulnerable Kerbals CodeForces
    D. Domino for Young
    C. Long Beautiful Integer
    B. Modulo Equality
  • 原文地址:https://www.cnblogs.com/macil/p/2554312.html
Copyright © 2020-2023  润新知