In an MVC 3 application, you can get the current page URL using the Request.Url
property. Here is an example of how to do it:
public ActionResult Index() { string currentUrl = Request.Url.AbsoluteUri; return View(); }
In the example above, we define an Index
action method, which returns a view. Within the action method, we get the current URL by accessing the Url
property of the Request
object, which returns a Uri
object. We then call the AbsoluteUri
property of the Uri
object to get the full URL as a string.
Note that Request.Url
returns the URL of the current request, which may not necessarily be the same as the URL of the current page being displayed to the user. For example, if the user submits a form, the URL of the current request will be the URL of the form submission, not the URL of the page that contained the form. To get the URL of the current page being displayed to the user, you can use JavaScript to read the value of the window.location.href
property.
"MVC 3 get current page URL in controller"
string currentUrl = HttpContext.Request.Url.AbsoluteUri;
"MVC 3 get current page URL in view"
string currentUrl = Request.Url.AbsoluteUri;
"MVC 3 get current page URL with parameters"
string currentUrlWithParameters = Request.Url.PathAndQuery;
"MVC 3 get current controller and action name"
string currentController = ControllerContext.RouteData.Values["controller"].ToString(); string currentAction = ControllerContext.RouteData.Values["action"].ToString();
"MVC 3 get current page URL in global filter"
string currentUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
"MVC 3 get current page URL with protocol"
string currentUrlWithProtocol = HttpContext.Request.Url.ToString();
"MVC 3 get current page URL without query parameters"
string currentUrlWithoutParameters = Request.Url.GetLeftPart(UriPartial.Path);
"MVC 3 get current page URL in Razor view"
string currentUrl = Request.Url.AbsoluteUri;
"MVC 3 get current page URL from ActionExecutingContext"
string currentUrl = filterContext.HttpContext.Request.Url.AbsoluteUri;
"MVC 3 get current page URL with encoded parameters"
string currentUrlWithEncodedParameters = HttpContext.Server.UrlEncode(Request.Url.ToString());
clang nonblocking pageviews pyqtgraph circular-permutations intentservice expert-system divide yii2 r-faq