automatically rewrite URLs in ASP.NET - asp.net

I use VS2010,C# to develop an ASP.NET web site, my customers want me to have their pages like this:
mysite.com/customer (in fact they call mysite/customer/default.aspx)
so I've manually created several folders for each customer, and inserted a default.aspx file in the folder so that users can view customer page by typing mysite.com/customer
is there a better way for performing this scenario? I don't want to have mysite.com/customer1.aspx, I want to have mysite.com/customer1, is there anyway that I can remove folders (and their containing default.aspx files) and generate something automatic using my customers database?
should I use URL rewriting? is there anyway that I can create page mysite.com/customer1.aspx, and users can view it by typing mysite.com/customer1?
I think it is possible to rewrite URLs in web.config, but I don't want to do it manually in web.config as my pages would increase in a daily basis
thanks

If you really really wanted to do this in code and not in the config you can rewrite the path yourself in the Application_BeginRequest using the RewritePath method of HttpContext
For example (and this is a very simplified example), if your aspx was in a subfolder the root it could go something like this:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (HttpContext.Current.Request.Path.StartsWith("/customer"))
HttpContext.Current.RewritePath("/customer/whatever.aspx");
}
Note: This ignores any query string which must be extracted and re-added and if you want it to be case-insensitive, you've have to handle that too.

Config-based URL rewrite is your best option. Look here: http://msdn.microsoft.com/en-us/library/ms972974.aspx . The rewriting is pattern based, so what you are describing (mysite.com/customer1.aspx to mysite.com/customer1) is possible.

ASP.NET Routing is the best way to do this, http://msdn.microsoft.com/en-us/library/cc668201.aspx
I've written a Navigation project that will help you as well, http://navigation.codeplex.com/ - if you're interested in this and need any help, let me know.

If you are (actually) willing to try url rewriting and such why not try ASP.net 4 Web Pages?
You can do Razor syntax in C# (cshtml) or VB (vbhtml).
You gain "pretty urls" by default (no configuration nor global.asax routing)
You can use Visual Studio or even WebMatrix
Possible challenges:
the (new) syntax - but its not that difficult - mostly # instead of <%=..%>
if you're more comfortable with ASP.Net controls, then it may prove to be more difficult than much simpler...

Related

Can MVC handle regular URL path requests, too?

ASP.NET MVC newbie question:
I've set up a MVC site, with a few controllers. Now my site also has a lot of content files, which are stored in a network of subfolders within my web site, and I need to be able to access them directly, e.g.
http://mydomain.com/Content/Images/Geography/Asia/Japan/TokyoAtNight.jpg
Is there a way to make this a direct pass-through to the content folder, as specified by the path, or do I have to make a Content controller that interprets the rest of the URL and returns the file as some kind of ActionResult? Bear in mind, of course, that there will be lots of different content types, not just JPEGs.
Thanks for your help!
This should work without you doing anything - static files are not processed by the routing engine.
You want to look into Routing, and IgnoreRoute specifically. Here are a couple of places to start.
Asp.Net Routing: How do I ignore multiple wildcard routes?
http://www.asp.net/mvc/tutorials/asp-net-mvc-routing-overview-cs
Take a look at the #Url.Content() helper method.
Url.Content("~Content/Images/Geography/Asia/Japan/TokyoAtNight.jpg")
Yes.
The IRouteHandler and the route registration in your global.asax is your extensibility point for configuring how MVC handles url paths.
However, by default ASP.NET MVC will allow you to access image files directly, without any additional configuration.

is it possible to do URL re-write in asp.net?

I have developers working on a site for me, they have told me there is no possible way to re-write the URL so there is no second level category e.g.
Remove "/category" from this URL http://www.somesite.com/category/page.aspx
I understand that page.aspx can simply go in the root folder on the server however I don't want to do this as adding heaps of pages will slow down server load time
Do not believe them... It's easy and can be done for example by UrlRewriter.net library which is easy to use and open source. Also they can write own rewriter which is also not more then an hour of work, for example like here.
check whether it will helpful for your requirement: URL Rewriting in ASP.NET

How to SEO friendly an existing ASP .NET 3.5 web application under IIS6

So, I know there's a lot of this subject here and over the Internet. But most articles/questions refers to "static" url rewriting, like:
www.site.com/products.aspx?category=Books
So they rewrite it to
www.site.com/Products/Books
That's ok but I need something else.
The site is like a CMS, it has different types of content.
Nowadays to read the article titled "How StackOverflow helps you in your development" you need to go to an URL like the following.
www.site.com/viewContent.aspx?Id=1234
What I want to achive is:
www.site.com/Content/Articles/how-to-stackoverflow-helps-you-in-your-development
So as I understand, I need to involve ASP .NET in that, because first I need to retrieve the article (an its title of course) and then rewrite the URL.
But I'm wondering how the hell ASP .NET will know how to get that article if I go to that URL, it doesn't include the id anywhere...
So maybe I could accept something like
www.site.com/Content/Articles/1234/how-to-stackoverflow-helps-you-in-your-development
I'm kind of lost here really.. I've never done any URL Rewriting at all and I've googled a lot and I cannot find a way to do what I want. Maybe what I want is not called url rewriting??? I don't know...
The site is running under Windows 2003 Server, IIS6, ASP .NET 3.5 SP1
And of course, I need a free solution, cannot spend 100usd on the ISAPI mod (besides I don't know if that is going to do what I need).
Thanks to all and sorry if this is a duplicated question, but I couldn't find it.
EDIT: I don't need to support non-ASP.NET files (jpgs, gifs, etc) don't need to be rewritten. I just need to rewrite the viewContent page to include the content title into the URL.
You can use the new Routing that comes with ASP.Net 3.5 sp1 to have clean URLs.
This can even be done in web forms and not just MVC. ( I have done it myself). See here and here for exanples of how to set it up.
You can throw the Id of the article and the title in the URL and make the Id the real parameter that gets used to search for the article. That is what SO does. Try removing the question for the URL and it will take you to the same place.
Even if you don't use the Id you can pass the title of the article "how-to-stackoverflow-helps-you-in-your-development" to your DB and retreive the article based on the title.
With regards to IIS 6 it is a little trickier since IIS 6 by default can't handle extension-less URL's.
There are a few work arounds:
Use the wildcard mapping in IIS to map all requests to Asp.Net
Put Default.aspx at the end of your urls
See this post for other possible solutions.
Although the first solution may have performance issues if all content in your site goes through Asp.Net (even images, css, .html ...) in a small site it shouldn't matter. I have used this approach and there wasn't any major performance issues. I think it is the simplest solution. Here is the website I built with it
I hope this helps.
This one is free, I have used it and it works pretty well: http://www.codeplex.com/IIRF
The nice thing is that it will handle url's without an extension (i.e. .aspx, .html, etc.)
You can achieve this with ASP.NET routing.You can do this with ASP.NET MVC as well as Webforms.No need to do anything with IIS.
Check the below link
https://web.archive.org/web/20201205221404/https://www.4guysfromrolla.com/articles/051309-1.aspx#postadlink
I did URL routing in my web application within 1 hour with the details from the above link.Its quite simple to learn .They provide sample codes too.It will help you to do it easily
You can retrieve the Id of the content using the title.But title should be unique.You can use ajax to check whether the title is a already existing one when user takes mouse out from the textbox.
Easiest way is to add a http module to your current webforms project.
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
This shows you the basics of it, But it can easily be expanded so that the pages you want to rewite is taken from a database or even built on the fly.
ASP.net MVC is your friend for that
stackoverflow use MVC

How to construct expandable website structure?

HI,
I have a ASP.NET webiste I created from craft and it now look a big mess. I want to reorganize this but don't know the good way to do it. Some first look well but later cause trouble with master page, image path...
Now I'm thinking of 2 ways:
Using UrlWriter: but it seems lead to a bulk of path rewrite and usually lead to Resource not found or something
Using a page as main entry and using Server.Tranfer to pull the right page content, despite of its location
Which is better? Do you have another method?
Please help!
There's another approach, System.Web.Routing, added in ASP.NET 3.5 SP1. Basically, you implement the IRouteHandler interface and manually route the request to an appropriate handler.
This is how ASP.NET MVC handles request routing. There's a guide here that uses it for Web forms.
By the way, consider looking at ASP.NET MVC and check if it's appropriate for your situation.

ASP.NET URL rewriting not working with folder URLs

I wrote a custom URL rewriting module, to take certain paths and map them to our catalog out of the database.
I am using the technique outlined in the link at the bottom of my post.
I'm using .NET 3.5. The problem is that it only works when there is a page name on the end of the original URL, but if the page name is left off, then it never even calls my handler.
So for example, the URL
http://mysite.com/folder/index.aspx works but
http://mysite.com/folder/ does not.
The one with just the folder never gets into my handler class at all. Is there something you need to do to enable folders to work properly?
http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/
Is a know issue when using .NET rewriting. You need to correctly configure the wildcard mapping. Here is a good link:
http://devtalk.dk/2007/03/19/Wildcard+Mapping+And+URL+Rewriting+On+IIS7.aspx
Another option will be rewriting by IIS ISAPI with regular expressions.
In my company we actually use both.
Check out this link for references:
http://www.kowitz.net/archive/2006/09/15/url-rewriting-using-ihttpmodule-vs-isapi-rewrite.aspx
I'm not sure how to solve it but the reason is becaus IIS is treating it as a directory (which it is) and doesn't have any knowledge about how to pass the folder to the ASP.NET isapi filter and trigger the .Net handler.
You could try doing it with IIS7 and inserting the handler in the actuall IIS pipeline. Or check the bottom of the article you referenced below the heading "Using RewriteModule".

Resources