.Net MVC 篡改get请求的参数

简单点说 就是把原来的参数给截取出来删除掉,然后把新的参数的url给拼接出来,最后通过HttpContext.Current.RewritePath(newUrl); 来执行新的地址
至于post请求 最近忙没有时间研究 后面有时间了研究补充一些,有哪位大神知道的可以共享哈哈~~

也可以通过这个方法类实现不同的跳转,即根据不同的请求参数RewritePath(newUrl)到不同的action中 执行不同的业务

 

做法 : 直接在Global.asax.cs 文件中加入下面的方法:

void Application_BeginRequest(object source, EventArgs e)
        {
            string area= HttpContext.Current.Request.Params.Get("area");
            string username= HttpContext.Current.Request.Params.Get("username");
            string passwor= HttpContext.Current.Request.Params.Get("password");
            string oldUrl = HttpContext.Current.Request.RawUrl;
 
            int startend = oldUrl.IndexOf("area=");
            if (startend >= 0 && "area".Equals(area))
            {
                string newUrl = string.Empty;
                string beginPart = string.Empty;
 
                beginPart = oldUrl.Substring(0, startend);
                int endPartStart = oldUrl.Substring(startend).IndexOf("&");
                if (endPartStart > 0)
                {
                    string endStr = oldUrl.Substring(startend).Substring(endPartStart);
                    if (endStr.StartsWith("&"))
                    {
                        endStr = endStr.Substring(1);
                    }
                    newUrl = beginPart + endStr;
                }
                else
                {
                    if (beginPart.EndsWith("?"))
                    {
                        newUrl = beginPart.Substring(0, beginPart.Length - 1);
                    }
                    else
                    {
                        newUrl = beginPart;
                    }
                }
                newUrl += "&area=" + ToolHelper.getNewArea(area);
                HttpContext.Current.RewritePath(newUrl);
            }
        }

  

 

posted @ 2018-03-20 10:47  樊金龙  阅读(483)  评论(0编辑  收藏  举报