首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# .net中的Url重写

问C# .net中的Url重写
EN

Stack Overflow用户
提问于 2011-07-27 12:43:03
回答 1查看 1.4K关注 0票数 0

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

下面是我想要发生的事件的流程:

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

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

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

下面是我的代码:

代码语言:javascript
复制
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() { }

    }
    }
EN

回答 1

Stack Overflow用户

发布于 2011-07-27 12:50:11

如果使用的是IIS7,则可以使用IIS7 URL Rewrite extension

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6839513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档