tags:

views:

147

answers:

4

Let me state off the bat that I'm not that familiar with ASP.Net MVC, and I don't think I have time for this project to become familiar and switch.

I'm building a website where there's only one physical page, mysite.com/default.aspx. If a user browses to mysite.com/foo/bar, I (somehow) want my default.aspx to handle that request. I know it sounds very "route"/"controller" oriented...but is there some way to do this without switching over whole-hog to MVC?

Also of note is that the site will also have static images and things that I don't want served up by my page...so the resulting html of mysite.com/foo/bar may have html that includes an img tag with a src of mysite.com/images/foo.gif, so I need to be able to preclude certain folders/files/whatnot from being processed.

+4  A: 

What you're talking about is called URL rewriting and yes, an ASP.Net forms application is capable of it. This entry seems to explain the technique fairly well here:

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

To allow HTTP rewrites to work on files that do not exist in IIS 6 you'll need to implement wildcard mapping. ASP.Net MVC falls prey to this same issue so even though you're not using ASP.Net MVC this article is still relevant:

http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx

Scroll down to the heading: "IIS6 Extension-less URLs"

If you need to figure out how to get to the IIS property window displayed scroll up a bit for some context. Just ignore the part about .mvc extensions.

Spencer Ruport
This is sort of working...but how do I get whatever module it is to pick up requests for files that don't physically exist?
Jonas
There is a setting for this in IIS but I believe it will cause a slowdown as it requires all requests to be processed by the ASP.Net ISAPI dll. I'll edit my post with a walk through on this in a moment.
Spencer Ruport
A: 

You could probably set up IIS to redirect all 404 requests to your website. If I remember correctly, there's a bug in IIS 6 (if that's what you're running) that will return a 200 status code, allowing you to do your normal processing.

Otherwise, you could use the tag in your web.config to handle the redirect--however, I think this might return the 404 error code.

Hope that helps!

Joel.Cogley
A: 

I think I have something working, in IIS7...but it seems kind of brittle:

I have an HttpModule, setup like this:

<modules runAllManagedModulesForAllRequests="true">
  <remove name="ScriptModule" />
  <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="MyRewriteModule" preCondition="managedHandler" type="MyWeb.MyRewriteModule, MyWeb" />
</modules>

and my module has code as follows:

void context_BeginRequest(object sender, EventArgs e)
{
  HttpApplication app = (HttpApplication)sender;
  HttpRequest request = app.Context.Request;
  List<string> ignoreExtensions = new List<string>() { "axd", "gif", "ico" };
  if(ignoreExtensions.TrueForAll(s => !request.FilePath.ToLower().EndsWith(s)))
    app.Context.RewritePath("~/default.aspx", request.Path, request.QueryString.ToString(), true);
}

Obviously I would cache and expand that list...but overall it seems to work. Can anyone point out any obvious drawbacks?

Jonas
A: 

Just modify your 404 error pages to redirect to default.aspx.

Kolten