Is it impossible for routing existing url by using Asp.net Routing?

让人想犯罪 __ 提交于 2020-01-24 00:49:12

问题


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,


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/1006318/is-it-impossible-for-routing-existing-url-by-using-asp-net-routing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!