生成文件路径并修整“.aspx”

Generating a file path and trimming ".aspx"

我想生成页面文件路径,然后 trim.aspx...我已经尝试了下面的代码,但这不起作用。 Windows 世界的新手,我已经习惯了 Linux 所以请保持友善 :D

编辑:我的以下代码的错误是:编译器错误消息:CS1525:表达式术语“[”无效,

但是一旦修复,它就会不断吐出错误。我相信我可能只是弄错了这项任务的基本原理。

public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
    string thepath = Page.ResolveUrl(Request.RawUrl);
    string thepath = Trim([.aspx] trimChars);
    string[] a = thepath.Split('/');
    for (int i = 0; i < a.Length - 1; i++)
    {
        thepath += a[i].ToLower() + "/"; ;
    }
    Canonical.Text = "<link rel=\"canonical\" href=\"https://example.com" + thepath.ToLower() + "\" />\n";
}
}

[.aspx] 无效,您没有修剪任何内容。将其更改为以下内容:

string thepath = Page.ResolveUrl(Request.RawUrl);
thepath = thepath.Replace(".aspx","");

这将根据您的需要执行,但如果您在字符串中的其他任何地方有 .aspx,它也会替换那里。如果你只想结束,你仍然可以使用 TrimEnd 但恕我直言,它有点老套。

string thepath = Page.ResolveUrl(Request.RawUrl);
thepath = thepath.TrimEnd(".aspx".ToCharArray());