如何在webmethod中获得没有扩展名的URL文件名



我有这个URL (mydomain.com/mypage.aspx)。我想只在webmethod中获得文件名"mypage"。我尝试了下面的代码,但我只得到函数本身的名称(MyMethod)。我没有从URL中获得页面的名称。如有任何帮助,不胜感激。

编辑更具体地说,webmethod驻留在用户控件中,因此使用FilePath或PhysicalPath会给我UserControl文件名(MyUserControl)的名称,而不是URL中的aspx页面(mypage)。

mypage.aspx

<asp:Content ID="Content" ContentPlaceHolderID="ContentPlaceHolder" runat="server">
<uc:myUserControl runat="server" /> 
</asp:content>

myUserControl.cs

public static string PageName { get { 
    return (string)(Path.GetFileNameWithoutExtension(
        Convert.ToString(HttpContext.Current.Request.Url)));
} }

public static string PageName { get {
    return (string)(Path.GetFileNameWithoutExtension(
        HttpContext.Current.Request.RawUrl));
} }

public static string PageName { get {
    return (string)(Path.GetFileNameWithoutExtension(
        HttpContext.Current.Request.PhysicalPath));
} }
[WebMethod]
public static string MyMethod()
{
    StringBuilder SBstring = new StringBuilder();
    SBstring.Append(PageName);
    return SBstring.ToString();
}

像这样使用FilePath:

        var fi = new FileInfo(HttpContext.Current.Request.FilePath);
        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fi.Name);

所需结构的完整工作解决方案(母版页->内容页->用户控制

)

网站主

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebTester.Site1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
        <div>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
    </form>
</body>
</html>

Webform1.aspx:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebTester.WebForm1" %>
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</asp:Content>

Webform1.aspx.cs:

namespace WebTester
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        [WebMethod]
        public static string ProcessIT(string name, string address)
        {
            return WebUserControl1.ProcessIT(name, address);
        }
    }
}

WebUserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebTester.WebUserControl1" %>
<script type="text/javascript">
    function HandleIT() {
        var name = document.getElementById('<%=txtname.ClientID %>').value;
            var address = document.getElementById('<%=txtaddress.ClientID %>').value;
            PageMethods.ProcessIT(name, address, onSucess, onError);
            function onSucess(result) {
                alert(result);
            }
            function onError(result) {
                alert('Something is wrong.');
            }
        }
</script>

<div>
    <p>Please enter data:</p>
    Name<br />
    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
    <br />
    Address<br />
    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnCreateAccount" runat="server" Text="Signup" OnClientClick="HandleIT(); return false;" />
</div>

WebUserControl1.ascx.cs:

namespace WebTester
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public static string ProcessIT(string name, string address)
        {
            var fi = new FileInfo(HttpContext.Current.Request.FilePath);
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fi.Name);
            string result = string.Format("Name '{0}' and address '{1}' came from Page '{2}'", name, address, fileNameWithoutExtension);
            return result;
        }
    }
}

最新更新