Issue with IHttpHandler and relative URLs - asp.net

I've developed a IHttpHandler class and I've configured it as verb="*" path="*", so I'm handling all the request with it in an attempt of create my own REST implementation for a test web site that generates the html dynamically.
So, when a request for a .css file arrives, I've to do something like context.Response.WriteFile(Server.MapPath(url)) ... same for pictures and so on, I have to response everything myself.
My main issue, is when I put relative URLs in the anchors; for example, I have main page with a link like this Go to Page 1 , and in Page 1 I have another link Go to Page 2. Page 1 and 2 are supposed to be at the same level (http://host/page1 and http://host/page2, but when I click in Go to Page 2, I got this url in the handler: ~/page1/~/page2 ... what is a pain, because I have to do an url = url.SubString(url.LastIndexOf('~')) for clean it, although I feel that there is nothing wrong and this behavior is totally normal.
Right now, I can cope with it, but I think that in the future this is gonna bring me some headache. I've tried to set all the links with absolute URLs using the information of context.Request.Url, but it's also a pain :D, so I'd like to know if there is a nicer way to do these kind of things.
Don't hesitate in giving me pretty obvious responses because I'm pretty new in web development and probably I'm skipping something basic about URLs, Http and so on.
Thanks in advance and kind regards.

First of all I would take a look at the output HTML delivered to the browser and specifically the links that you are describing.
You say that the link is Go to Page 2 but according to your result I would guess it is more like Go to Page 2.
You can confirm this by placing a brakepoint in the handler and when it triggers with "~/page1/~/page2" look in the address bar of your browser and it should say something like "http://www.example.com/page1/~/page2"
You should first look at the code generating the link. If it is generated from some kind of function call, make sure you get the web address and not the script address.
In any case these kind of links that switch between first level pages should all start with a "/" indicating that their location is relative to the root of your website rather than relative to the current shown page.

Related

Redirecting to .aspx in a different domain

Okay, I'm dealing with a problem hear.
I'm building landing pages for my company.
The main website works with a .aspx form to retrieve car data (from for example licenseplates).
Now we've set up some new relevant domain names to use for some of the landing pages.
The problem now is, that when on those pages I type in licenseplate and click search, it fails doing so.
Since it tries to find the .aspx form on the landing page domain url.
For example:
Main site: www.mysite.com/category.aspx?k=80zbfk (refered to when the licenseplate is typed in.)
Landing page: www.mysite2.com/category.aspx?k=80zbfk (were it refers to on the landing page)
No the second one should refer to the first one. But I can't seem to find a way to do so.
I don't have acces to the .aspx files since they're in control of a external company.
Is there any way to fix this? To refer the landing page to the .aspx from the main site?
Or do I have to contact the webcompany to ask for the files so I can copy them to the other domain?
Thanks in advance!
It sounds like you want to be using absolute links rather than relative.
An absolute link includes the host and path of the target url, whereas a relative one contains only the path and lets the browser infer the host (via the url being visited).
For example, if you were editing the HTML of a page here on Stack Overflow, the absolute link to this question would look something like this,
http://stackoverflow.com/questions/26379795/redirecting-to-aspx-in-a-different-domain
While the relative one would look like this,
/questions/26379795/redirecting-to-aspx-in-a-different-domain
In the case of the latter one, the browser would be left to assume based on context that you wanted to go to that path on http://stackoverflow.com/. There's a bit more to it, and variations on that syntax exist. But that's the gist of it.
So, getting back to your question, yes. You will probably have to update the ASPX pages. Relative links are best practice in most cases, which explains why they were used in your code, but you've got an exception. It's probably going to be easiest to just go through and change whatever links you need, to point to the main domain. But for what it's worth, that should be a relatively easy fix, once you get the files.
Alternatively, you could set up a rewrite rule or redirection policy on your landing page servers to automatically 301 redirect any requests that contain search information off to the main server, but that's definitely a workaround approach, and there will be a performance hit in doing so. The one and only advantage that I can imagine to doing that is that you wouldn't need to get the pages from the third party, but it sounds like that wouldn't be a bad idea to do anyway.

Find the actual page opened from the URL in the asp.net

HI so i keep running across websites which when looked through or searched (using their own search function) return's a static URL ie.) ?id=16 or default.aspx no mater what page of the website you visit after the search has been performed. This becomes a problem when i want to go directly to a post/page within one of these sites so i'm wondering. If anyone knows How could i actually find out what the absolute URL is.
So that i can navigate straight to it. I'm not really familiar with coding but have tried looking in the page source but i wasn't really able to gleam anything from there.
The basics around asp.net urls: http://www.codeproject.com/Articles/142013/There-is-something-about-Paths-for-Asp-net-beginne
It all really depends on what you're trying to find, as far as finding a backway to locate a absolute path, is highly doubtful. If the owner of the site(most blogs) want you to have a perma link to a page, they use url-rewriting for putting things in the URI like title page and such. Alot of MVC sites do this now.
The '?id=16' you're seeing is just a query string, a holder for other logic they are doing.

Show constant URL for site in asp.net

I have a web site with number of pages, developing in asp.net.
I have a page URL's like:
example:
1) http://www.xyz.com/Home.aspx
2) http://www.xyz.com/Index.aspx
3) http://www.xyz.com/viewMember?Name=abc&id=1
But the end user is at any page, i would like to show the URL like "http://www.xyz.ie".
Is there any setting in web.config ? If not, is there any other way ?
Please help me...
Thanks in advance.
Jagadi
You can not keep one single URL for different page - but you can do some tricks to simulate it.
To make the url stay the same, but the content change, you need to make some trick.
I am not recommend, search engines they will not follow what you do and they show each page different, user can not make bookmark, and average user can easy find the real url of the page, even with one different click on the browser can find it.
One trick is to use frames, or iframes. On the main page you load all the rest inside an iframe, or inside a frame.
Second trick is to use ajax to load each other content.
And finally you can use session to know what to show on the user, user did not change links, but make post back that change the content.

iframed ASP actions trouble

This is actually a follow up on my previous question (link)
I've created the HttpHandler and it works fine for now, I'll add flexibility by using the querystring and session to point the post I'm making in the right direction.
The next question is as follows.
Now that I have the old page iframed as it should be, there's still the trouble of handling the postbacks (or actions) these pages trigger.
Every button action (asp form post) refers to a page that is not there (it's on the other server from which I am importing functionality).
I've tried using a url mapping to the other server but I get an error that tells me the external link is not a valid virtual directory. Hence I discarded this option.
I there anyway to keep functionality going inside the iframe?
please do ask clarification if you need it.
I got a solution from a colleague.
before passing the response string to the Iframe from the handler I use a string.replace to adjust the urls in the old site. This way they point to the old site and everything works again :)

re-rendering a site within an iframe?

I want to make a site where there user can basically navigate the web from within an iframe. The catch is that I'd like to be able to have more control over what is rendered within the iframe. Specifically,
I'd like to be able to filter out images or text, disable forms etc.
I'd also like to be able to gather feedback such as what links the users clicked on.
Question 1:
Is this even possible using a standard back-end scripting language (like php), with html and javascript on the frontend?
Question 2:
Would I first need to grab the source of the site before it is rendered, then do whatever manipulation is necessary, and finally re-render it somehow?
Question 3:
Could somebody please explain the programming flow that would occur here (assuming its possible)?
I think you would probably want to grab the source of the of site (with server-side code) before rendering it. You might run into cross-site scripting issues if you try to use JavaScript. Your iframe would load a page like render.php and pass the address of the page to render os a querystring parameter. Then use regular expressions to find elements in the HTML that render.php downloads from the address. Rewrite the HTML as necessary and then write it all out to the iframe.
Rewrite links so that that the user is taken to a page you control and redirected onto a target site if you want to track where people are going. Example: a link in the page needs to go to google.com. You would send them to tracker.php?target=http://google.com. You control tracker.php and can log each load of this page and then redirect the user to the target site.
Update:
Another possible solution is to use Apache or other server to proxy the target website. There are modules like mod_proxy for this. There may also be modules that let you parse the HTML or you could roll your own.
I should point out that even the best solutions offered to your question will be somewhat brittle if you do not have full control over the target site. You will want to have lots of error handling or alerting.
You can have a look at this. It uses iFrame really well, and maybe even use the library it has.

Resources