从 JS 的 master 获取正确的 *.aspx 页面

Get correct *.aspx page from master for JS

我有一个母版页,其中一些脚本调用网络方法。我的内容页面继承自中间库class,它实现了[WebMethod] void SetSomeParam(bool flag)。问题是母版页中的脚本不知道,他需要 URL,~/default1.aspx/SetSomeParam~/default2.aspx/SetSomeParam
如何从母版页获得正确的 URL(请不要使用代码隐藏)。


更新:<%=Page.ToString()%>/SetSomeParam 提供类似 ASP.pages_default1_aspx/SetSomeParam 的输出,但工作 URL 必须是 default.aspx/SetSomeParam

您可以像这样在母版页 JS 中创建一个有效的 url:

var url = document.URL.replace(/\/?$/, '/') + 'SetSomeParam';

您可以使用表单操作。

您的页面中应该有这样一个表单元素:

<form id="form1" action="default1.aspx" method="post" name="form1">

您可以通过以下方式检索操作:

document.getElementById("form1").action

有两种方法可以在客户端获取页面名称。

  1. 从window.location.pathname

    中提取
    var url = window.location.pathname;  
    var myPageName = url.substring(url.lastIndexOf('/') + 1);      
    alert(myPageName);  
    
  2. 使用 Path.GetFileName 共 Asp.net

    var page = '<%=Path.GetFileName(Request.Path)%>'