English 中文(简体)
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:

  1. Run everything through the .NET engine with a wildcard application extension mapping.

  2. 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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签