【问题标题】:ASP.NET Dynamic C# commandASP.NET 动态 C# 命令
【发布时间】:2016-09-12 06:32:04
【问题描述】:

我想为自己上课,我正在编写这段代码,但我碰壁了

public static string url(string example)
{
    string back = "";
    if(example == "random1")
    {
        back = "Request.Url.AbsolutePath.ToString()";
    }
    return back;
}

我想在我的 asp 网络项目示例中使用我的 Request.Url.AbsolutePath.ToString() 命令 > default.aspx.cs > url("random1");

我已经搜索了很长时间,抱歉英语低:/

【问题讨论】:

  • 只需要去掉 Request.Url.Absolute.ToString() 周围的双引号
  • img这个问题
  • 您有 System.Web 的 using 语句吗?如果您在上课,您可能必须使用完整路径:System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToString()
  • 谢谢它正在工作,我可以将此方法用于其他 C# 命令吗?
  • 是的,在需要时。您只需在 using 语句中包含要使用的库,在本例中为 System.Web。

标签: c# asp.net variables dynamic command


【解决方案1】:

你能用这样的东西吗:

public static string url(string example)
{
    string back = "";
    if (example == "random1")
    {
        back = HttpContext.Current.Request.Url.AbsolutePath.ToString();
    }
    return back;
}

甚至更短:

public static string url(string example)
{
    return example == "random1" ? HttpContext.Current.Request.Url.AbsolutePath : "";
}

【讨论】:

  • 谢谢,如果我想构建一个动态方法,只需像这样的公共静态无效示例()我想在 AI 不知道 txt 文件中获取方法命令,或者像这样我可以使用什么?
  • 实在看不懂你在问什么:/
猜你喜欢
  • 2011-07-27
  • 2010-12-19
  • 1970-01-01
  • 2015-07-18
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多