C# .net 中的 URL 重写

发布于 2024-11-26 06:10:22 字数 3237 浏览 0 评论 0原文

如何重写 url,使其在地址栏中显示类似 subdomain.example.com/blog 的路径,但显示诸如 www.example.com/blog/?tag=/subdomain 的页面。

这是我想要发生的事件的过程:

首先:我导航到 subdomain.example.com/blog 第二:我被重定向到 www.example.com/blog/?tag=/subdomain 第三:地址栏中的 url 仍然显示 subdomain.example.com/blog 我位于 www.example.com/blog/?tag=/subdomain 页面。

我更喜欢使用 HttpContext.RewritePath() 方法,

我一直在尝试在 IHttpModule 中对此进行编码,但没有成功,

这是我的代码:

using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;

namespace CommonRewriter
{
    public class ParseUrl : IHttpModule
    {
        public ParseUrl()
        {

        }

        string req = null;
        string rep = null;

        public String ModuleName
        {
            get { return "CommonRewriter"; }
        }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);
            application.EndRequest += new EventHandler(application_EndRequest);
            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
        }

        void application_AuthorizeRequest(object sender, EventArgs e)
        {

        }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {


        }

        private string ParseAndReapply(string textToParse)
        {
            string final = null;

            if (textToParse.Contains("example.com"))
            {
                string[] splitter = textToParse.Split('.');

                if (splitter[0].ToLower() != "www" && (splitter[2].ToLower()).Contains("blog"))
                {
                    string add = splitter[0].Remove(0, 7);
                    final = ("http://www.example.com/blog/?tag=/" + add);
                }
                else { final = textToParse; }
            }
            else { final = textToParse; }

            return final;
        }

        void application_BeginRequest(object sender, EventArgs e)
        {
            req = HttpContext.Current.Request.Url.AbsoluteUri;

            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;


            if (req.ToLower().Contains("example.com/blog") && !req.ToLower().Contains("www."))
            {
                string[] split = req.Split('.');
                if (split[1] == "example")
                {
                    rep = ParseAndReapply(req);

                    context.RewritePath(rep);

                    context.Response.End();
                }

            }
        }


        void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpContext context1 = HttpContext.Current;

            if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("/?tag=/")) 
            {
                context.RewritePath(req,false);
            }

            }
        }

        public void Dispose() { }

    }
    }

how can i rewrite a url so that it shows a path like subdomain.example.com/blog in the address bar but displays a page such as www.example.com/blog/?tag=/subdomain.

here is the process of events I want to occur:

first: I navigate to subdomain.example.com/blog
Second: I am redirected to www.example.com/blog/?tag=/subdomain
Third: the url in the address bar still displays subdomain.example.com/blog even though
I am at the www.example.com/blog/?tag=/subdomain page.

I would prefer to use HttpContext.RewritePath() method

I have been trying to code this in a IHttpModule without success

here is my code:

using System;
using System.Web;
using System.Net;
using System.Text;
using System.IO;

namespace CommonRewriter
{
    public class ParseUrl : IHttpModule
    {
        public ParseUrl()
        {

        }

        string req = null;
        string rep = null;

        public String ModuleName
        {
            get { return "CommonRewriter"; }
        }

        public void Init(HttpApplication application)
        {
            application.BeginRequest += new EventHandler(application_BeginRequest);
            application.EndRequest += new EventHandler(application_EndRequest);
            application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
            application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
        }

        void application_AuthorizeRequest(object sender, EventArgs e)
        {

        }

        void application_PreRequestHandlerExecute(object sender, EventArgs e)
        {


        }

        private string ParseAndReapply(string textToParse)
        {
            string final = null;

            if (textToParse.Contains("example.com"))
            {
                string[] splitter = textToParse.Split('.');

                if (splitter[0].ToLower() != "www" && (splitter[2].ToLower()).Contains("blog"))
                {
                    string add = splitter[0].Remove(0, 7);
                    final = ("http://www.example.com/blog/?tag=/" + add);
                }
                else { final = textToParse; }
            }
            else { final = textToParse; }

            return final;
        }

        void application_BeginRequest(object sender, EventArgs e)
        {
            req = HttpContext.Current.Request.Url.AbsoluteUri;

            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;


            if (req.ToLower().Contains("example.com/blog") && !req.ToLower().Contains("www."))
            {
                string[] split = req.Split('.');
                if (split[1] == "example")
                {
                    rep = ParseAndReapply(req);

                    context.RewritePath(rep);

                    context.Response.End();
                }

            }
        }


        void application_EndRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            HttpContext context1 = HttpContext.Current;

            if (HttpContext.Current.Request.Url.AbsoluteUri.Contains("/?tag=/")) 
            {
                context.RewritePath(req,false);
            }

            }
        }

        public void Dispose() { }

    }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯淡〆 2024-12-03 06:10:22

如果您使用的是 IIS 7,则可以使用 IIS 7 URL 重写扩展

If you are using IIS 7, you can use the IIS 7 URL Rewrite extension

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文