【问题标题】:Get Current.Request.Url in mvc application having a .aspx extension在具有 .aspx 扩展名的 mvc 应用程序中获取 Current.Request.Url
【发布时间】:2013-06-10 17:07:32
【问题描述】:

我有一个 ASP.NET MVC 3 应用程序,我必须在其中将带有 .aspx 扩展名的请求映射到另一个路由。我想要做的是在应用程序启动中获取当前请求 url。但问题是它在所有没有 .aspx 扩展名的 url 中运行良好,但在 ex 的 url 中 (http://example.com/Products/5/16/Eettafels.aspx) 它只显示http://example.com/

但是http://example.com/Products/5/16/Eettafels 显示正确的路径..

所有代码都是简单的一行:

string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();

谁能知道我做错了什么

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

虽然这是一篇很老的帖子了。

我只是粘贴 Ha Doan 链接的代码,以便任何人更容易解决这个问题。

string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx

string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx

string host = HttpContext.Current.Request.Url.Host;
// localhost

查看此 SO 以对此进行讨论

【讨论】:

    【解决方案2】:

    现在,您可以覆盖在调用任何 Action 之前执行的 Controller 方法。然后我建议您保留当前的 ​​Url,例如在 ViewBag 或 TempData 中建议的 @Ifitkhar,以防您打算重定向,然后稍后在您想要返回的 Action 中使用它。

    public class ProductsController
    {
        protected override void OnActionExecuting(ActionExecutingContext filterContext){
            TempData["previousUrl"] = HttpContext.Current.Request.Url.AbsoluteUri;
            base.OnActionExecuting(filterContext);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2013-10-10
      • 2011-05-06
      • 2013-08-26
      • 2013-05-10
      • 1970-01-01
      • 2011-08-06
      • 2019-06-26
      • 2015-10-07
      相关资源
      最近更新 更多