Asp.Net System.Web.Routing Find actual .aspx Page - asp.net

I'm using System.Web.Routing to have some better URL's and have come across a problem. I need to know the actual page that's handling the request.
for example a request comes in as:
/basketball/home
I need to find the page that handles that request, like:
/management/default.aspx
I'm only using the System.Web.Routing and not MVC. I have a handle to the RequestContext that contains some of the route information, but i don't see what i need.
Thanks in advance.
******* UPDATE *******
I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

There is actually another simple way to get the actual page:
String vPath = ((System.Web.Routing.PageRouteHandler)Page.RouteData.RouteHandler).VirtualPath
Do not forget to check Page.RouteData.RouteHandler is not null - while you are getting the page w/o ASP.Net routing but directly.

Can you not retrieve this from the current HttpContext object?
Perhaps something like this:
public string GetCurrentPageName()
{
string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
string sRet = oInfo.Name;
return sRet;
}
UPDATE:
Have you tried this article?
How to: Construct a URL from a Route
You should be able to retrieve it back from the Routing table you have constructed.

vhinn terrible's answer worked...
Page.AppRelativeVirtualPath
You just have to remove the initial tilde ("~"), and you're ready to go.
var path = Page.AppRelativeVirtualPath.Replace("~", String.Empty);
I don't know why it was downvoted. Worked for me like a charm.

Try using this code:
Page.AppRelativeVirtualPath

I was able to use Context.CurrentHandler which give me "ASP.management_default_aspx", not exactly the page but enough to get the page name.

Related

Is there an equivalent to GetRouteUrl() at application/class level (.Net 4.8)?

Within classic webforms ASPX pages, I create URL routes using the following:
var url = this.GetRouteUrl("MyRouteName", new {UserId = 123}); // Generates /UserId/123
MyRouteName is defined within the Routes.RegisterRoutes() method used at startup.
However, I need to generate a URL within a helper method that lives at application-level. There is obviously no page context there, so I get an error.
The MSDN documentation states:
This method is provided for coding convenience. It is equivalent to
calling the RouteCollection.GetVirtualPath(RequestContext,
RouteValueDictionary) method. This method converts the object that is
passed in routeParameters to a RouteValueDictionary object by using
the RouteValueDictionary.RouteValueDictionary(Object) constructor.
I read these, but cannot figure out whether what I need to achieve is possible. An online search revealed some answers, but these are many years old an not easy to implement.
The following works to generate the equivalent of GetRouteUrl at application/class level:
var url = RouteTable.Routes.GetVirtualPath(null,
"MyRouteName",
new RouteValueDictionary(new { UserId = 123 })).VirtualPath;
Remember that only returns a local Url (e.g. /UserId/123) so if you need to domain name you'll have to prepend that as well:
var url = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + RouteTable.Routes.GetVirtualPath(null,
"MyRouteName",
new RouteValueDictionary(new { UserId = 123 })).VirtualPath;
I have in my Global.asax file in the Application_Start
RouteTable.Routes.MapPageRoute("Level1", "{lvl1}", "~/Routing.aspx");//Any name will do for the aspx page.
RouteTable.Routes.MapPageRoute("Level2", "{lvl1}/{*lvl2}", "~/Routing.aspx");
Then my Routing.aspx.cs page handles the logic for what will happen with the request.
Mainly I Server.Transfer to an aspx page which will display the requested page.
Routing.aspx page picks up any "non-existing" page.
Hope this helps or at least gives you some more ideas.

ASP.net link to other page with passing parameter

my question is, how to call another aspx page from same project, with passing parameter, like ID-20?
Thanks.
In the source page, you can pass data with query string
.../target.aspx?Id=20
Then on target page get those values
String s = Request.QueryString["Id"];
Notice that you shouldn't pass any sensitive data by this method
Response.Redirect( "another.aspx?id=20" );

How do I identify the referrer page in ASP.NET?

In VS2003, I am trying to find out the particular page where the request is coming from. I want to identify the exact aspx page name.
Is there a way to only get the page name or some how strip the page name?
Currently I am using the following instruction...
string referencepage = HttpContext.Current.Request.UrlReferrer.ToString();
and I get the following result...
"http://localhost/MyPage123.aspx?myval1=3333&myval2=4444;
I want to get the result back with out any query string parameters and be able to identify the page MyPage123.aspx accurately...
How do I do that??
Instead of calling .ToString on the Uri, use the AbsolutePath property instead:
string referencepage = HttpContext.Current.Request.UrlReferrer.AbsolutePath;
This should get you "/MyPage123.aspx" in your case.
Edit: Had LocalPath instead of AbsolutePath by mistake
Look at the Segments property of the URI class (which is what HttpContext.Current.Request.UrlReferrer returns).
Something like HttpContext.Current.Request.UrlReferrer.Segments[1] (changing the 1 indexer to get the correct segment you require).

How to get original url after HttpContext.RewritePath() has been called

I am working on a web app which makes use of a 3rd party HttpModule that performs url rewriting.
I want to know if there's any way to determine the original url later on in Application_BeginRequest event. For example...
Original url:
http://domain.com/products/cool-hat.aspx
Re-written url (from 3rd party httpmodule):
http://domain.com/products.aspx?productId=123
In the past I have written HttpModules that store the original url in HttpContext.Items but, this is a 3rd party app and I have no way of doing that.
Any ideas would be appreciated.
Try this:
string originalUrl = HttpContext.Current.Request.RawUrl;
The original URL is inside of this property.
I had the same problem, but I wanted the fully qualified URL (RawUrl gives you just the Path and Query part). So, to build on Josh's answer:
string originalUrlFull =
Page.Request.Url.GetLeftPart(System.UriPartial.Authority) +
Page.Request.RawUrl
I know this question was asked long time ago. But, this is what I use:
System.Uri originalUri = new System.Uri(Page.Request.Url, Page.Request.RawUrl)
Once you have the URI you can do a ToString() to get the string, or cal any of the methods/properties to get the parts.
Create a new HttpModule to serve as a wrapper around (inherits) the third party module and do whatever you want with it.
In your case, override the appropriate function (ProcessRequest?) and store the original url in HttpContext.Items, and then call the MyBase implementation. Should work fine.

how can an .ASPX page get its file system path?

I have a page something.aspx, with associated codebehind something.aspx.cs. In that codebehind, I want to know the filesystem location of something.aspx. Is there any convenient way to get it?
Update: I got several excellent answers, which unfortunately didn't work because of something else crazy I'm doing. I'm encoding some additional information on the URL I pass in, so it looks like this:
http://server/path/something.aspx/info1/info2/info3.xml
The server deals with this OK (and I'm not using querystring parameters to work around some other code that I didn't write). But when I call Server.MapPath(Request.Url.ToString()) I get an error that the full URL with the 'info' segments isn't a valid virtual path.
// File path
string absoluteSystemPath = Server.MapPath("~/relative/path.aspx");
// Directory path
string dir = System.IO.Path.GetDirectoryName(absoluteSystemPath);
// Or simply
string dir2 = Server.MapPath("~/relative");
Request.PhysicalPath
Server.MapPath is among the most used way to do it.
string physicalPath = Server.MapPath(Request.Url);
Server.MapPath( Request.AppRelativeCurrentExecutionFilePath )

Resources