【问题标题】:How to hide page name and querystring in ASP.NET?如何在 ASP.NET 中隐藏页面名称和查询字符串?
【发布时间】:2013-11-01 07:59:49
【问题描述】:

我想在 asp.net 中设置 URL Masking 以隐藏 URL 中的页面名称和查询字符串。

目前我正在设置以下代码以在全局应用程序文件中执行 url 重写。

routeCollection.MapPageRoute("Login", "Login", "~/frmLogin.aspx");

但我想重写 URL,使其仅向最终用户显示域名。 http://www.domainname.com - 像这样

请帮我设置一下。

提前致谢,

【问题讨论】:

  • 从性能角度考虑,最好按照以下答案中的建议将您的页面设置为默认页面。

标签: asp.net iis-7 asp.net-4.0 asp.net-4.5


【解决方案1】:

您可以将 frmLogin.aspx 页面设置为 Web 服务器中的默认页面。

如果您使用的是 IIS 7,步骤如下:

 1.Select your website
 2. In description click on default document
 3. Add your page (frmLogin.aspx) in and set its priority.

【讨论】:

  • Chirag - 这似乎非常适合您的场景。每当有人在他们的浏览器中写入您的域名时,都会提供默认页面。从性能角度来看它是最好的,因为它在 IIS 处理管道中很早就出现了。试试吧.. 如果您有不同的 IIS 版本,您可以随时 google(或)询问我们是否需要进一步的帮助。请确保将您的页面置于 IIS 默认页面列表的顶部。
【解决方案2】:

我们在应用程序中使用以下方法在域上简单地显示标志页。其他页面也可以修改。

在 Global.asax 中:

routeCollection.MapPageRoute("SIGNIN", String.Empty, "~/signin.aspx");

【讨论】:

  • 设置默认页面的成本应该低于 mappageroute,尽管两者都应该有效。
【解决方案3】:

如果您使用域掩码,则无需更改代码并获得相同的结果。

【讨论】:

  • 如何设置域掩码?
  • 您与您的域名注册商合作。
【解决方案4】:

出于实验目的,我尝试了以下方法。所以我不知道它在回发的复杂页面上会如何表现。

当您请求 www.domainname.com 时,实际请求将转到 www.domainname.com /default.aspx 或您设置的任何其他默认页面。在默认页面加载中,第一件事是检查任何名为“pagetoview”的会话,如果已设置,则 server.transfer 到该页面,否则服务器默认页面。

现在假设用户从页面转到 form.aspx'。 form.aspx 加载方法应检查 pagetoview 会话变量是否与当前页面名称相同,然后取消设置并继续,否则将 pagetoview 变量设置为当前页面名称并重定向到域。

那里会检查默认页面并会发生 server.transfer。希望您对这种奇怪的方法有所了解。

【讨论】:

    【解决方案5】:

    您应该使用 cookie 和用户控件模拟 asp.net 路由。 所以我们只有一个名为 default.aspx 的 aspx 文件,其他页面应该放在用户控件中。 将此脚本放在 default.aspx 的末尾:

    <script src="https://code.jquery.com/jquery-2.2.0.min.js" type="text/javascript"></script>
    <script>
        $(document).ready(function () {
            $("a").click(function (e) {
                e.preventDefault();
                var attrHref = $(this).attr("href");
                $.getJSON("/service.asmx/SetRouteCookie", { href: attrHref }, function (e) {
                    window.location.reload();
                });
            });
        });
    </script>
    

    此脚本禁用所有链接行为,然后我们手动处理点击事件。 在单击事件中,我们通过 ajax 调用 Web 服务方法。该服务设置了某个 cookie 来保存当前页面:

    [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, XmlSerializeString = false, UseHttpGet = true)]
        public void SetRouteCookie()
        {
            if (HttpContext.Current.Request.QueryString["href"] != null)
            {
                string href = HttpContext.Current.Request.QueryString["href"];
                HttpCookie c = new HttpCookie("CurrentRoute", href);
                c.Expires = DateTime.Now.AddHours(1);
                HttpContext.Current.Response.Cookies.Add(c);
    
                HttpContext.Current.Response.ContentType = "application/json";
                HttpContext.Current.Response.Write("{\"status\":\"ok\"}");
    
            }
        }
    

    在创建 cookie 并成功回调后,我们通过 javascript 重新加载页面。 在默认的 Page_Load 事件中,我们加载适当的用户控件:

    protected void Page_Load(object sender, EventArgs e)
        {
            #region process route
    
            if (HttpContext.Current.Request.Cookies["CurrentRoute"] != null)
            {
                var route = HttpContext.Current.Request.Cookies["CurrentRoute"].Value.ToString();
                string pageName = GetPageName(route);
                Placeholder1.Controls.Add(LoadControl("/ctrls/" + pageName + ".ascx"));
            }
            else
            {
                Placeholder1.Controls.Add(LoadControl("/ctrls/default.ascx"));
            }
    
            #endregion
    
        }
    
        public string GetPageName(string href)
        {
            int index = href.IndexOf("&");
            if (index == -1)
                return href.Trim('/');
            else
            {
                return href.Substring(0, index).Trim('/');
            }
        }
    

    我在 git 上创建了示例代码: HideRoute

    【讨论】:

      【解决方案6】:

      你应该使用 Server.Transfer 方法 例如,您在 default.aspx 中有 asp.net 按钮 像这样写事件点击:

       Server.Transfer("/login.aspx?q1=testQuery");
      

      使用此方法,您的 url 不会更改,并且在 login.aspx 中您可以获取查询字符串

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-06
        • 2011-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        相关资源
        最近更新 更多