2013-11-01 81 views
4

我想在asp.net中设置URL Masking来隐藏URL中的页面名称和查询字符串。如何在ASP.NET中隐藏页面名称和查询字符串?

目前我设置下面的代码来执行全局应用程序文件中的URL重写。

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

但我想重写URL的方式,它只会向最终用户显示域名。 http://www.domainname.com - 这样的

请帮我设置它。

由于提前,

+0

设置你的页面,默认页应该是最好的,因为在下面的答案建议,考虑到性能的角度来看。 – Arvin

回答

1

如果使用域掩蔽则没有更改代码,你得到同样的结果。

+0

我们如何设置域掩码? – Chirag

+0

你可以使用你的域名注册商来做到这一点。 – smoore4

1

我尝试了以下实验目的的方法。所以我不知道它如何在复杂的页面上回传。

当您申请www.domainname.com时,实际请求将转到www.domainname.com /default.aspx或您设置的任何其他默认页面。在缺省页面加载中,第一件事是检查任何名为say'pagetoview'的会话,如果它被设置,那么server.transfer到那个页面,其他服务器是默认页面。

现在让我们说一个用户从页面转到form.aspx'。 form.aspx加载方法应检查pagetoview会话变量(如果它与当前页面名称相同,然后取消设置),并继续将pagetoview变量设置为当前页面名称并重定向到域。

那里的默认页面会检查并发生server.transfer。希望你用这个奇怪的方法得到一些意见。

2

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

在Global.asax中:

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

设置默认页面应该比mappageroute更便宜,尽管两者都应该工作。 – Arvin

1

你应该模拟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> 

这个脚本禁用所有链接的行为,然后我们处理手动点击事件。 在click事件中,我们使用ajax调用Web服务方法。该服务设置了一定的cookie来保存当前页面:创建cookie,并成功的回调,我们由JavaScript刷新页面后

[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\"}"); 

     } 
    } 

。 上默认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

1

您应该使用Server.Transfer方法 比如你在默认情况下有asp.net按钮。ASPX 写事件的点击这样的:

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

用这种方法,您的网址不会改变,在login.aspx的你可以让你的查询字符串

3

您可以设置frmLogin.aspx页作为默认页网络服务器。

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

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

Chirag - 这对你的情况来说很完美。只要有人在您的浏览器中写入域名,就会提供默认页面。从性能的角度来看它是最好的,因为它在IIS处理管道中很早。试试吧..如果你有一个不同的IIS版本,你可以随时谷歌(或)问我们是否需要进一步的帮助。请确保将您的页面放在IIS默认页面列表的顶部。 – Arvin

相关问题