Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net-mvc

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

like image 963
Manish Gautam Avatar asked Jun 14 '13 05:06

Manish Gautam


People also ask

Can you use ASPX pages in MVC?

If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks.

What is the default extension for MVC URL?

MVC works with Extensionless URLs only (by default)<system.

How do I get the current URL for my razor?

Inject NavigationManager in razor. Use Uri from NavigationManager to get the current URL.

How can I get current URL in asp net core?

To get the current URL I am using the following C# instruction: Uri address = new Uri(Request. Host.


1 Answers

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

like image 139
Iftikhar Ali Ansari Avatar answered Oct 13 '22 12:10

Iftikhar Ali Ansari