views:

198

answers:

2

Hi,

I am using Context.RewritePath in Application_BeginRequest to make my url user friendly, everything works fine on my local machine but on the server(shared) i get 404 errors. do you have any idea how can i fix this problem?

thanks

A: 

Which IIS version are you running? 6?

As far as I know the URL that you want to map, must be physical existing in order to make this work.

Example: /Page/Television/default.aspx should map to /page?id?=5

You need to create the Folder Page/Televsion and the default.aspx in your solution. The default.aspx must not contain more than "<% Page %>

citronas
A: 

Under Cassini, Application_BeginRequest runs for all files. Under IIS, it only runs for files with managed handlers, such as *.aspx files.

For the general case, you will need to create your own HttpModule. Here's an example (based off of a similar one from my book: Ultra-Fast ASP.NET):

using System;
using System.Web;

namespace Samples
{
    public class RewriteExample : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += OnBeginRequest;
        }

        void OnBeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            // re-write URL here...
        }

        public void Dispose()
        {
        }
    }
}

Then register it in web.config (this is for IIS; using Cassini is slightly different):

<system.webServer>
  <modules>
    . . .
    <add name="RewriteExample" type="Samples.RewriteExample" />
  </modules>
</system.webServer>
RickNZ
How do I create an HttpModule? Could you provide an example please?thanks
Example added...
RickNZ