开发者

Url Rewriting not working on server

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?

tha开发者_开发技巧nks


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 %>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜