简体   繁体   中英

Get Current.Request.Url in mvc application having a .aspx extension

I have a ASP.NET MVC 3 application in which I have to map a request with .aspx extension to another route. what i am trying to do is to get the current request url in application start. but the problem is it runs fine with all urls without .aspx extension but in a url for ex ( http://example.com/Products/5/16/Eettafels.aspx ) it shows only http://example.com/

however with http://example.com/Products/5/16/Eettafels it shows the correct path ..

All the code is a simple line:

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

Can any one have any idea what i am doing wrong

though it is a very old post.

I am just pasting the code Ha Doan linked to so that it will be easier to anyone landing on this question.

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

Check this SO for discussion on this

Nowadays you can override a Controller method that executes before any Action is called. What I'd then suggest is you keep the current Url like @Ifitkhar suggested in a ViewBag, or TempData, in case you intend on redirecting, then using it later in the Action you want to return afterwards.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM