views:

80

answers:

2

From my last question, I have been found that Asp.net Routing can't route with existing url for more info read this.

How to redirect existing url to another url by using Asp.net Routing?

Hint!

I think, this behevior like access denied behevior that always redirect to default login page.

Thanks,

A: 

You should make sure the -existing- url is 'found' / 'matched' in the route table, and the url to which you are redirecting is not.... (unless you are redirecting to a Controller Action / Route)

Ropstah
I use this pattern("{*anything}").
Soul_Master
A: 

I found solution. It's very easy by using HttpModule. Please look at my following example code.

In global web.config

<configuration>
    <system.web>
        <httpModules>
            <add name="RedirectModule" type="MySolution.RedirectModule, MySolution" />
        </httpModules>
    </system.web>
    <!-- The following code will be used by IIS 7+ -->
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
            <remove name="RedirectModule" />
            <add name="RedirectModule" type="MySolution.RedirectModule MySolution" />
        </modules>
    </system.webServer>
</configuration>

In MySolution/RedirectModule.cs

using System;
using System.Web;

namespace MySolution
{
    public class RedirectModule : IHttpModule
    {
        #region IHttpModule Members

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(HttpApplication_BeginRequest);
        }

        public void Dispose() {}

        #endregion

        void HttpApplication_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (*[logic for checking before redirect]*)
            {
                app.Response.Redirect(*[url]*);
            }
        }
    }
}

For more infomation, Please look at Url Mapping source code like UrlRewritingNet.UrlRewrite.

PS. IHttpModule is very powerful interface. It can handle with every request type. So, It can help me to completely solve this question.

Soul_Master