我有一个ASP。. NET MVC 3应用程序,其中我必须将带有.aspx扩展名的请求映射到另一个路由。我要做的是在应用程序启动时获得当前请求url。但问题是,它运行良好的所有url没有。aspx扩展,但在url为ex(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();
有谁知道我做错了什么吗
虽然这是一个很老的帖子。
我只是把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来讨论这个
现在你可以重写在任何Action被调用之前执行的Controller方法。我然后建议的是你保持当前的Url像@Ifitkhar建议在一个ViewBag,或TempData,如果你打算重定向,然后在你想要返回之后使用它的动作。
public class ProductsController
{
protected override void OnActionExecuting(ActionExecutingContext filterContext){
TempData["previousUrl"] = HttpContext.Current.Request.Url.AbsoluteUri;
base.OnActionExecuting(filterContext);
}
}