Manish Gautam
Manish Gautam

Reputation: 516

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

Upvotes: 5

Views: 39660

Answers (2)

Tiramonium
Tiramonium

Reputation: 867

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);
    }
}

Upvotes: 0

Iftikhar Ali Ansari
Iftikhar Ali Ansari

Reputation: 1760

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

Upvotes: 16

Related Questions