One custom handler for different request url's in ASP.NET web site
I am doing a small ASP.NET 2.0. site refactoring.
Currently there is the following folder structure:
/siteroot/services/home/service1
/siteroot/services/home/service2
/siteroot/services/home/service3
...
all the folders (service1, 2, 3) contain an almost identical default.aspx, with the different开发者_如何学JAVA content hardcoded into the web form declaration.
My idea is to create one service.aspx and serve custom page (with the same template) on a particular URL request.
My question is:
How can I redirect all request to ../services/home/service1,2,3 to one particular handler/aspx page?
Preferably not to have those folders in the project structure at all, but intercept a requests that are headed to them.
If you are able to implement ASP.NET 3.5 (runs on the 2.0 CLR), then Routing sounds perfect for what you need to achieve. With this you can intercept URLs based on a particular format and then route them through to a single page or handler. You won't need a folder structure for each of your services.
Here's a few links on how to get going with it in ASP.NET WebForms. It's pretty quick to implement:
- http://msdn.microsoft.com/en-us/magazine/dd347546.aspx
- http://msdn.microsoft.com/en-us/library/cc668201%28v=VS.90%29.aspx
- http://haacked.com/archive/2008/03/11/using-routing-with-webforms.aspx
- 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
One way:
- Create a custom handler class that implements
IHttpModule
- In
Init(HttpApplication application)
, register a method forapplication.AuthorizeRequest
- In this method, parse
((HttpApplication)sender).Request.Path
as appropriate, usingHttpContext.Current.RewritePath
if necessary to point to a "real"service.aspx
page.
Then register your custom handler in <system.webServer><modules>
.
Another way is to use the MVC-style routing, which works without MVC just fine. Details are in the answer to the question here: custom URL Routing in asp.net.
精彩评论