Getting Current Page URL From Master Page with Routing - c#

I'm trying to be able to determine which page url the user is loading from the master page.
So far I've been able to use
this.Request.RawUrl
to get the path of the page itself, which works fine for most cases.
However in this particular website, we use a lot of complicated routing, so something like (say)
/Product/5/2/Purchase
might redirect to /Purchase.aspx?ID=5Type=2
of which I'd want the actual aspx file path.
I've also tried this.Request.PhysicalPath, but that doesn't give the route and basically just appends the path the user requested to the virtual directory.
So how can I do it?

An asp.net Page treats a master page as just another control.
So if you want to get the page, you can always use the Page property provided by the MasterPage class.

I used this and worked :
Page.ToString().Replace("ASP.","").Replace("_",".")

Related

Only load page when it comes from certain pages?

Webform ASP.NET. Currently I am using Response.Redirect() to redirect to another page when I press a button or navigate. Is there a way to validate from where it comes the redirection?
Lets say I have Login.aspx and when I press the button Save, it redirects to Account.aspx but I want to check from where it comes. If someone just writes in the browser Account.aspx, that person could just enter to that page and I don't want that.
Is there a way to validate the Redirection to not be null and that it comes from an specific page?
EDIT to clarify my problem:
Lets say I have an Index.aspx and a menu Registration.aspx. The registration is just a form to save the user data and after that it redirects to Buythings.aspx I want the guy to access Buythings.aspx only from a certain page redirection (Registration.aspx)
You need to request.referrer; Something like this (you may have to edit it a bit):
string pageName = Request?.UrlReferrer?.Segments[1];
Then you say:
if (pageName ="Registration.aspx")
{ DO STuff}
Use Http referer (wiki)
Request.UrlReferrer (SO discuss) is the actual property which is available as part of the http spec.
However, as mentioned in the wiki, there are certain cases it may not work or may get tampered with, so you have to take care of those.
Controller actions are essentially methods so you could pass something along that acts like a password, not sure I would recommend this. Instead if your trying to restrict access I would authenticate against the user to make sure they have permission to access the page, if you want anyone logged in to be able to access the page then just use
User.Identity.IsAuthenticated

Redirecting users to same dynamic page in asp.net.

I am working on a dynamic website.
I am able to redirect users to a page using the code in web.config
add `url="~/baked-foods" mappedUrl="~/food_type.aspx?PID=1"/></pre>`
This works perfect for me and takes to www.food.com/baked-foods
Now I want to use another url like www.food.com/baked-foods/
It should take me to the same page of
www.food.com/food_type.aspx?PID=1
I have added one slash at the end. I want to have both urls to be redirected to the same page.
Kindly suggest me a best solution to handle this.
You just add another <add> child element to the <urlMappings> element.

Generic Urls for set of asp.net internal pages

I am looking for a solution that will allow me to print generic url for a set of pages.
Example:
For pages - site.com/About/Contact.aspx, site.com/About/WhoWeAre.aspx, site.com/About/Members.aspx etc., user should see only site.com/About/ in the address bar.
Is that something achievable? This site is not SEO friendly and requires users to login before accessing content, also I don't expect site.com/About/ or any internal pages to resolve to any page when typed directly on browser. I am also fine if real url is printed on status bar if the user hovers on the internal links. I don't think URL Rewrite or URL Routing works here. Or may be I am missing something. Using .NET 3.5 and C#.
I am tagging sitecore because I am more interested in sitecore based solution where I have different nodes under About tree and I want users to see only upto /About in the address bar, but I think if it can be done in asp.net, I can figure out sitecore part.
As you are interested in sitecore, I can tell you how we have done custom URL in a recent sitecore build.
You need to have a custom link manager by extending the current LinkProvider.LinkBuilder sitecore class. Then you need to add your custom logic on how you would like the URL by overriding GetItemUrl and BuidlItemUrl to display URL and finally and it in the web.config for sitecore to use your custom link manager.
Absolutely, use the asp.net 4 routing engine.
This will give you exactly what you want:
http://weblogs.asp.net/dotnetstories/archive/2011/01/03/routing-in-asp-net-4-0-web-forms.aspx
EDIT: for 3.5 check out:
Friendly URLs for ASP.NET
The only way to solve this, keeping /About in the address bar, would be to create client-side code that switches out the content based on user selection.
You can do that by loading the About page and then load the content for any child pages using a postback or using ajax calls.
You could also put an iframe on the /About page, and have the navigation links point the iframe to the other pages.
I think you might be able to use the target attribute to target the iframe, or use javascript to change the src of the iframe.

Is it possible to load .ascx page in iframe

Is it possible to load ascx page in iframe.When I try it I got a error
The type of page you have requested is not served because it has been explicitly forbidden. The extension '.ascx' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
But when I want to load .html page in same location it can be load.
May i did any thing wrong ?Thanks.
You can't load .ascx file in an iframe for the same reason you can't load it directly in the browser. It's not a page, but rather a control. So it's meant to be used within an .aspx page.
ascx is for usercontrols.
It isn't supported to request an ascx you have to create a page (aspx) for it. Another approach is to create an HttpHandler and manipulate the response.
.ascx-file is not a page it is a user control. What you need to do is either make a page of it, that is, an .aspx-file and change it accordingly, or wrap it in an .aspx-file that includes your .ascx-file.

append query string to URL on event (C#)

my client has a website that currently makes requests on a particular event (click, load etc). the website is working well and all but he has a problem with getting the website statistics with Google Analytics, the reason is, because the website never does a re-direct to a different page, it is all within the same page and no matter what event is loaded in the website(be it a video, tables etc) everything are displayed under the same url
www.somewebsite.com/default.aspx
what I want to achieve is on a particular event, change the url to
www.somewebsite.com/default.aspx?type=abc&id=999
How can I do this,. what is the easiest method to do this? please advise. The whole website is built in C#.
Many Thanks.
Is this event happening on the server or the client?
If it's on the server, you can call Response.Redirect and add your new query string parameter to the current url.
If it's on the client (Javascript), you can set the location property.
If you want to preserve your page's state, try adding your querystring parameter to the form's action parameter in Javascript.
Alternatively, as jeffamaphone suggested, you can change the hash (the part after the # sign) by setting location.hash without posting back to the server.
Actually, you should probably move some of the elements to different pages... this is based on what you said:
because basically all I am doing is
hiding and showing elements based on
events, if i do a response.redirect,
it will reload teh homepage.
If I understand correctly, the user is never redirected to a different page, you are just hiding/unhiding elements from default.aspx based in query strings... correct? the simplest solution will be to split that content into different aspx pages.

Categories