我有这个网址(mydomain.com/mypage.aspx)。我想获取文件名" mypage"只在网络方法中。我尝试了以下代码,但我只获得了函数本身的名称(MyMethod)。我没有从网址获取该网页的名称。任何帮助都很受欢迎。
修改 更具体地说,webmethod驻留在用户控件中,因此使用FilePath OR PhysicalPath会在URL中为我提供UserControl文件名(MyUserControl)的名称,而不是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)));
} }
OR
public static string PageName { get {
return (string)(Path.GetFileNameWithoutExtension(
HttpContext.Current.Request.RawUrl));
} }
OR
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();
}
答案 0 :(得分:0)
只需像这样使用FilePath:
var fi = new FileInfo(HttpContext.Current.Request.FilePath);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fi.Name);
所需结构的完整工作解决方案(母版页 - &gt;内容页面 - &gt;用户控制
Site Master
<%@ 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;
}
}
}