URL handling in ASP.NET WebForms - asp.net

What's the best approach to provide URL handling in an ASP.NET application to produce results similar to Craigslist. Specifically, I'm looking to handle something like "newyork.mysite.com" and "california.mysite.com", along with parameters such as "newyork.mysite.com/products/hardware". Based on the "location" (newyork.), my goal is to change the site's look and feel and filter it's data results -- standard stuff I would think.
At the moment, ASP.NET MVC is not an option for the site (from what I've read, this sort of functionality is something that MVC excels at), but I can use .NET 4.0. Also, I was wondering what roles DNS would play in this scenario. Is the subdomain just a wildcard or are there any special considerations I would need to make?
The majority of my web development experience has been internal, intranet applications, so URL handling like this is not something I've ever really needed to do. I'd appreciate any advice (best practices, pitfalls, etc) as to how to approach this.

You can avail of the ASP.NET 4.0 routing engine to create friendly URLs:
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
You can create URLs like:
california.mysite.com/home
california.mysite.com/about
california.mysite.com/products
As regards "california.mysite.com", the "california" part represents a sub-domain. This is set up by adding an A record in your host admin and setting up IIS 7 to recognize it.

Related

Is there a canonical way to create a Site Index in ASP.NET MVC?

By Site Index I don't mean SiteMap as in a way to expose a XML. But a classic A-Z book index-like to offer all of the contents(links) of your site.
Is there a best practice way to do this in ASP.NET MVC (=>4) ?
In short; No.
If your site is database driven, you might be able to query your database for a list of pages/items, but you'd still need to work out the page URLs based on your routing set-up and AFAIK there is no built-in way to do this.

MVC5 subdomain routing

I am putting together a simple website and I want to have a single subdomain. It will look like
www.sitename.com - a landing/marketing page
app.sitename.com - the meat of it
How can I do this in MVC5 with the least amount of pain? I have seen several solutions, such as Is it possible to make an ASP.NET MVC route based on a subdomain? and http://attributerouting.net/#subdomains, but they seem to be much more complicated than I need and I am hoping someone has done it before.
Also, I am developing using IIS Express but some of the stuff I have found has said express can't handle this so I would like to know if that's possible as well.
Thanks

How to create a blog in ASP.NET and not ASP.NET MVC

I have created a blog application with ASP.NET MVC and MSSQL. I must say, i really enjoyed the process of creating an application with ASP.NET MVC. Clean URLS(with URL routing), No view States and so on.
BUT i was wondering how would this would have been done if i choose web-form style coding? will the aspx would be created in the fly as i create a article.(take this url for ex: http://weblogs.asp.net/scottgu/archive/2010/10/06/announcing-nupack-asp-net-mvc-3-beta-and-webmatrix-beta-2.aspx) though the URL is not clean but still makes sense. if yes then what about its corresponding cs file? if no how is the URL so clean?
Would be great if someone throw some light on how it is done in ASP.NET.
Thank you, Faraaz.
If your using .NET 4, you can make use of MapPageRoute to accomplish the clean URL's with regular Web Forms.
routes.MapPageRoute("MySuperCleanRoute", "some/clean/url", "~/ActualPage.aspx");
With the addition of MapPageRoute, you no longer have the feeling "I should use ASP.NET MVC because i want clean URL's".
Choose ASP.NET MVC if you like the pattern. If your used to Web Forms, use it - and use MapPageRoute to achieve clean URL's (or use a URL Rewriting module if <= .NET 4)
A blog in ASP .NET would have been done in much the same way. The idea is to use one file and URL rewriting. IIS7 has URL rewriting built in but for IIS6 you can use something like ISAPI_Rewrite to handle .htaccess style files (which is what Apache uses).
ASP .NET MVC handles all this for you in its routing but you can do it yourself using a URL rewriting tool. The difference is that for MVC, the application handles the rewriting but ISAPI_Rewrite, IIS Rewriting are done by the server. This can change a URL like http://mysite.com/something/other to http://mysite.com/file.asp?p1=something&p2=other.
The second link is only internal to the server (it doesn't actually change the URL in the user's address bar). In the case of a URL like on Scott's blog, you could store the 'announcing-nupack-asp...' bit in a database as part of the article row so your blog article page has something to look for. The files don't actually exist on the server but the rewriting passes all requests to an existing file with parameters.
Note that this technique is common for lots of different sites - not just blogs. Notice the Stack Overflow URL, Twitter URLs, etc.
MSDN has an old article on URL Rewriting in ASP .NET, along with some examples of filters you can use.
See IIS URL Rewrite Module
You can use the URL Rewriting that is in asp.net MVC with WebForms if you are using .Net Framework 3.5 SP1 (if you don't have access to .Net 4)
Here's an MSDN magazine article explaning...

How to efficiently create facebook-like url-rewriting in asp.net

I'm currently looking into url-rewriting and how it should be done right and hope you have some inputs here.
At the current stage of development only the kind of url-rewriting I'm interested in is adding facebook like behavior to businesses to which we provide services on our site, i.e. www.mysite.com/ShowBusinessInfo.aspx?id=1 should be rewritten to www.mysite.com/HostedBusinessName. - The idea is that when a business registers on our site they can choose whatever the last part of the url should be.
What would be the best way to support this feature? Custom IHttpModule, Global.asax (I'm afraid that this is too slow?), UrlRewriter.net/UrlRewriting.net or a completely different solution.
The site is developed in asp.net and runs on IIS 7.5.
I've actually done something like this before and this is the article I used as a resource: http://stweet.wordpress.com/2010/03/15/creating-a-new-website-programmatically-on-iis-using-asp-net/
This article will tell you how to programmically add a web site to your IIS using C#. Keep in mind that depending on how your IIS and DNS is setup, you may need to also modify your DNS server. You can find various scripts for doing this here: http://msdn.microsoft.com/en-us/library/ms682129%28VS.85%29.aspx
Hope this helps!
If you have fairly simply rewriting rules, I'd use an IHttpModule that attempts to match the URL's LocalPath property with a value in your DB, and then calls context.RewritePath(string).
If you have more complex stuff, then I'd start looking at UrlRewriting.NET or the routing options in ASP.NET.

Using hosting companies "404 error behavior" to implement URL Routing

Many hosting companies let you define which page will be shown to the user if the user goes to a page that does not exist. If you define some .aspx page then it will execute and be shown.
My question is, why not use this for routing. since I can parse the users URL and then do a server.transfer to the page I want. I checked and there is no redirect sent to the client and the http headers are HTTP/1.1 200 OK.
So, is this a good idea for servers that don't have ASP.NET 3.5 SP1 or if you are not using MVC?
Thanks
You "can" do that, but why not just create an HttpModule and handle the routing there? That's how most of the URL rewriting systems work (in actuality, it's also how the MVC routing works since global.asax is just a pre-build HttpModule with a few extras).
The big thing with relying on that kind of server handling you describe is that you really aren't in control of it, and it is a hackish mechanism... by that I mean you are taking a function of the web server that has a specific purpose and design and laying a different meaning and function on top of it... which means you now have no built in handling for an actual 404 error. Plus, the mechanism you are contemplating is harder to adapt to your purpose than just using the other options available to you.
Unless you need something special from routing, you should consider using an existing routing component such as Mod-Rewrite or one of the dozen or so other popular URL rewriters that were built before the MVC routing engine was implemented and work fine in older versions of asp.net. There are also numerous tutorials on using HttpModules, HttpHandlers, and various other techniques to do routing in asp.net webform environments.

Resources