【问题标题】:Is it possible to run php code at asp.net with c#是否可以使用 c# 在 asp.net 上运行 php 代码
【发布时间】:2020-05-25 05:59:39
【问题描述】:

在后台使用 c# 时是否可以在 asp.net 中使用一些 php 代码。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MeineWebseite.löschen.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">


<?php
echo "Hallo \"Welt\"";
echo $name;
echo " - i'am feeling fine";

function ausgabe ($wert) 
{ 
  echo $wert; 
}
?>

    </form>
</body>
</html>

我想以这种方式使用 php。我应该安装哪个软件包?

【问题讨论】:

  • 你试过了吗?
  • 是的,我做到了!我尝试使用 peachpie,但效果并不理想
  • 你不能只混合两种语言。对于初学者,选择一种语言并学习如何用它编写简单的程序。一段时间后,您可能会查看其他语言,然后了解它们各自的优缺点。
  • 好的!我现在明白了。谢谢

标签: c# php html asp.net


【解决方案1】:

就在IIS 下运行PHP 项目而言,这是有可能实现的,这里有一个关于SO 的answer,可能在这方面有所帮助。

但是,如果您想混合 ASP.NET C# 和 PHP 代码,那么我想这是不可能的。真正的问题是,你到底为什么要走这条路?有什么 PHP 可以做而 C# 不能做的事情吗?对我来说,C# 似乎比 PHP 更优雅,你为什么要这样做?

就能够在前端回显一些数据而言,您可以像这样在 C# 中执行此操作(或使用许多其他可能适合您的场景的方式):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MeineWebseite.löschen.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Literal runat="server" ID="ltEchos"/> <!-- Would share a sample to use this control to populate things -->
    </form>
</body>
</html>

.CS 代码隐藏:

public void EchoSomeDataWithResponseWrite()
{
    Response.Write("Hallo \"Welt\""); // echo, that does not involves the Literal control
    //  to give a line break, put your html like below
    Response.Write("<br />");
    var name = "John Doe"; // to write name
    Response.Write(name);

    // to write some raw script:
    Response.Write("<script>alert('Weather is fine today!')</script>");
}

public void EchoSomeDataWithLiteralControl()
{
    var name = "John Doe";
    var html = "Hallo \"Welt\" <br /> "; 
    html += name + "<br />";

    ltEchose.Text = html;
}

权力在您手中,您可以使用 C# 实现任何使用 PHP 可以实现的目标。

【讨论】:

  • 现在很多人都在使用 PHP,而且我知道它很容易使用。到目前为止,我一直在使用 c#,但我尝试随机显示图像形式的文件夹,但我做不到,但我在互联网上看到了很多使用 PHP 的示例
  • 您也可以找到许多使用 C# 显示图像的示例。万维网上有很多帮助材料。
猜你喜欢
  • 1970-01-01
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2015-03-05
  • 1970-01-01
  • 2011-08-08
  • 2021-06-22
相关资源
最近更新 更多