How to get the URL the client used - asp.net

In ASP.NET, how can I know whether the user has typed the default document in the URL or not—i.e., distinguish if the URL ends with / or /Default.aspx?

Nice problem :D unfortunately Request.URL.AbsoluteURI does not differentiate between the two in the default setup of IIS. IIS does a 'courtesy' redirect to the default pages existing in a web-directory, and this is done during protocol resolution -- i.e., ISAPI extensions do not see it (python framework , ASP.NET framework etc etc). You have two options :
Disable the courtesy redirect by unlisting default.aspx as default redirect, and then you will be able to use Request.URL.AbsoluteURI.
You will almost certainly not want to do this option.... Write an ISAPI filter to extract the data... perform a silent remapping (i.e., do not redirect to default.aspx, just remap the request headers') or when you redirect set a QueryString -- i.e., abc.com/default.aspx?redirected=1
p.s. this is what i get from three years of developing server plugins :/.

Related

Change the broswer's address name

how can I change the broswer's address name?? For example I have this: www.example.es/Pages/site.aspx and I want this: www.example.es/site
How can I do that?? Ty.
you can use the 404 error challenge and Server.transfer() to do URL rewriting using classic asp
Things you need
How to create a custom 404 page
Regular expression pattern to recreate actual file path or a predefined list
eg:-www.example.com/pg1/ ---> /pages/pg1.asp
Simple server.transfer() to handle valid / invalid server requests.
<%
IF Request.ServerVariables("SCRIPT_NAME") = "pg1" THEN
Server.transfer("/pages/pg1.asp")
ELSE
Response.write("404 - Page not found");
END IF
%>
that's all you are doing basic level URL rewriting using classic asp.
Your question asks about .aspx (asp.net) pages, but your question is tagged asp-classic... you will get better responses to future questions by using the correct tag.
In this case: as long as you can install a module on your server, and you're server can process ASP.Net's web.config files, you can use the IIS URL Rewrite module that Graham mentioned in the comments. I've got an old ASP-Classic site that uses the module just fine.
If you can install an extension, you could also use Ionics Isapi Rewrite Filter. Some hosts have this installed already.
All things being equal, if you have the ability to run your choice, I'd go with the IIS URL Rewrite Module as I've generally seen better performance from it. But that may just be me.
If you do not have that kind of access to the server, and there's not something already installd, you may have to go with the 404 mapping Jeevaka Nuwan Fernando mentions.
There are some other options, but they depend on if you are using ASP.Net or ASP-Classic.

How can I redirect a user to a new page in IIS based upon the URL

I have a web app that opens certain projects based upon a projectID
So ID 12345 = project foobar
http://my.example.com?projID=12345
However I would like to provide a different site that doesnt require the projectid, and is easier to remember for the end user.
So if a user visits
http://simple.example.com/foobar (note this is a different site, though I suppose we could put this in the same site if easier)
They would automatically get redirected to
http://my.example.com?projID=12345
Obviously I would want a system where I could have more entries than just foobar
if you don't want to use the rewrite module mentioned above you could always do it yourself by implementing a custom 404 handler and key off aspxerrorpath or perhaps putting the code in a custom httpHandler.
We actually do this with in one of our applications. We use a cms system to specify all of the redirects so nothing is really hard coded and marketers and SEO teams can handle it without our help.
in my opinion you should use ashx to handle web request. here is very simple and beautiful example of what you are looking for :-
http://www.dotnetperls.com/ashx

URL aliases: rewrite, routing, not sure

ASP.net 4.0 / IIS7.
I want to "alias" a single web form to appear as various extensionless urls. Given a form in my web root called "mySite.com/ColorWebForm456.aspx":
I want it served as multiple names, e.g., mySite.com/Color, mySite.com/Colour, mySite.com/Colors, mySite.com/Coler, etc., without creating folders and duplicate forms with those names.
I never want mySite.com/ColorWebForm456.aspx displayed in the browser, it needs to display as mySite.com/Color, even if the user somehow acquires it and types in that exact ~.aspx address.
The variations will account for several alternate or mis-spellings users might attempt - I don't want them "corrected", however. So, if a user types in mySite.com/Colour, the url is NOT rewritten to mySite.com/Color, but the same page is served via ColorWebForm456.aspx as the requested "mySite.com/Colour".
I've seen so many articles on this that I'm not even sure where this would be best handled: in Global.asax, IIS7 URL Rewrite, web.config, etc., and I'm not even sure this is technically a case of url rewriting or routing... ?
You can achieve this with ASP.NET 4 routing for web forms, or if you are using MVC its available too: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Except potentially for #3; it would work, but you would have to explicitly declare the variations you want. In that case, rewriting supports regular expression matching, so that might be better for what you are looking for.
I had issues setting up URL rewriting and various aspects of web forms, so be aware. There is some setup required for a rewrite module potentially (I could have done it wrong too), where URL routing is already built in and handled.
HTH.
If you have a rewrite engine available:
(using ISAPI Rewrite .htaccess syntax)
# redirect any requests for the filename back to the friendly URL
RewriteRule ^/colorWebForm4567.aspx(.*) /Color$1 [NC,R=302]
# rewrite /Color requests to the web form
RewriteRule ^/(Color|Colour)/(.*) /ColorWebForm4567.aspx$2
Following article expalins it all, but if you will be using a web host after you complete your application I would check with the hosting provider first to see if they are offering everything you need to have:
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Hope this helps!
As a side note: Search engines dislike it, if you publish content under different URLs ("duplicate content").
So my recommendation would be to stick with one (rewritten) URL, not multiple (rewritten) URLs for the same content.

What is the recommended method of HTTP Redirection from multiple URLs to one URL?

I have a website that has a number of URLs that people use to connect to that site (uses the bindings on the IIS website and everything works as intended):
http://www.sample.com
http://sample.com
https://www.sample.com
http://xyz.sample.com
http://oldurl.com
Now what I want to do is have all of the URLs go to https://www.sample.com - so if you type in "http://xyz.sample.com" or "sample.com" you should go to https://www.sample.com
The question is what is the best mechanism to do this? I have one possible solution (which I will put as an answer to this question), but I get the feeling that there might be another, better solution available.
One possible option via IIS settings would be to do the following:
Remove extra site bindings from website (i.e. xyz.sample.com, sample.com, etc...). This should leave just the web url you are trying to get all traffic to flow to (i.e. https://www.sample.com)
Create a second website in IIS
In the second website create bindings that were removed from the original website
In the second website use the HTTP Redirect (option in IIS) to direct all traffic from the second site (where all the alternate urls now reside) to the goal site (https://www.sample.com)
Set up a new website on the new binding https://www.sample.com, and then, on the old website, open the Http Redirect property page and set up your redirect.
That's in IIS Manager.

ASP.NET Friendly URLs

In my research, I found 2 ways to do them.
Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine was with a database view that contained all the friendly URLs and their mapped 'real' URLs). Now the trick is to get your requests run through the .NET engine without an aspx extension. The 2 ways I found are:
Run everything through the .NET engine with a wildcard application extension mapping.
Create a custom aspx error page and tell IIS to send 404's to it.
Now here's my question:
Is there any reason one of these are better to do than the other?
When playing around on my dev server, the first thing I noticed about #1 was it botched frontpage extensions, not a huge deal but that's how I'm used to connecting to my sites. Another issue I have with #1 is that even though my hosting company is lenient with me (as I'm their biggest client) and will consider doing things such as this, they are wary of any security risks it might present.
`#2 works great, but I just have this feeling it's not as efficient as #1. Am I just being delusional?
Thanks
I've used #2 in the past too.
It's more efficient because unlike the wildcard mapping, the ASP.NET engine doesn't need to 'process' requests for all the additional resources like image files, static HTML, CSS, Javascript etc.
Alternatively if you don't mind .aspx extension in your URL's you could use: http://myweb/app/idx.aspx/products/1 - that works fine.
Having said that, the real solution is using IIS 7, where the ASP.NET runtime is a fully fledged part of the IIS HTTP module stack.
If you have the latest version of IIS there is rewrite module for it - see here. If not there are free third party binaries you can use with older IIS (i.e. version 6) - I have used one that reads the rewrite rules from an .ini file and supports regular expression but I cant remember its name sorry (its possibly this). I'd recommend this over cheaping it out with the 404 page.
You have to map all requests through the ASP.NET engine. The way IIS processes requests is by the file extension. By default it only processes the .aspx, .ashx, etc extensions that are meant to only be processed by ASP.NET. The reason is it adds overhead to the processing of the request.
I wrote how to do it with IIS 6 a while back, http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx.
You are right in doing your mapping from the database. RegEx rewriting, like is used out of the box in MVC. This is because it more or less forces you to put the primary key in the URL and does not have a good way to map characters that are not allowed in URLs, like '.
Did you checked the ASP .Net MVC Framework? Using that framework all your URLs are automatically mapped to Controllers which could perform any desired action (including redirecting to other URLs or controllers). You could also set custom routes with custom parameters. If you don't have seen it yet, maybe it will worth the look.

Resources