ASP.NET代码无法运行,收到错误:无法找到资源

Mar*_*all -2 c# asp.net

我有以下代码,我希望只在我的浏览器中加载页面,用IIS 7运行它.

<%@ Page Language="C#" Debug="true" %>
<%
using System;

protected string callRotate()
{
    ProcessStartInfo info = new ProcessStartInfo();
    string[] arguments = { "arg1" , "arg2" };
    info.FileName = "ConsoleApplication1";

    Process process = Process.Start(info.FileName, arguments);
    Process.Start(info);
}
%>
Run Code Online (Sandbox Code Playgroud)

这是我在浏览器中遇到的错误:

    Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS1513: } expected

Source Error:


Line 1:  <%@ Page Language="C#" Debug="true" %>
Line 2:  <%
Line 3:  using System;
Line 4:  
Line 5:  protected string callRotate()

Source File: c:\inetpub\wwwroot\testing\testing.aspx    Line: 3 
Run Code Online (Sandbox Code Playgroud)

更新1: 现在我收到此错误:

Compiler Error Message: CS1519: Invalid token 'using' in class, struct, or interface member declaration

Source Error:


Line 1:  <%@ Page Language="C#" Debug="true" %>
Line 2:  <script runat="server">
Line 3:  using System;
Line 4:  
Line 5:  protected string callRotate()
Run Code Online (Sandbox Code Playgroud)

更新2:

错误:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0246: The type or namespace name 'ProcessStartInfo' could not be found (are you missing a using directive or an assembly reference?)

Source Error:


Line 5:  protected string callRotate()
Line 6:  {
Line 7:     ProcessStartInfo info = new ProcessStartInfo();
Line 8:     string[] arguments = { "arg1" , "arg2" };
Line 9:     info.FileName = "ConsoleApplication1";

Source File: c:\inetpub\wwwroot\testing\testing.aspx    Line: 7 
Run Code Online (Sandbox Code Playgroud)

以下是代码:

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System" %>
<script runat="server">

protected string callRotate()
{
    ProcessStartInfo info = new ProcessStartInfo();
    string[] arguments = { "arg1" , "arg2" };
    info.FileName = "ConsoleApplication1";

    Process process = Process.Start(info.FileName, arguments);
    Process.Start(info);
}
</script>
Run Code Online (Sandbox Code Playgroud)

更新3: 好的,以前我切换到使用命令行应用程序,因为我无法在浏览器中使用任何此代码,但现在您已经向我展示了如何切换回在浏览器中运行它.

所以我已经把我的应用程序转换为在浏览器中工作,一切都很好.但是如何获取url变量呢?

我知道这与Request.QueryString有关,但这不起作用,我添加了以下行:

当前代码:

<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Web" %>

<script runat="server">
static void Main(string[] args)
{
    string url = Request.QueryString["url"];
    string rotate_dir = Request.QueryString["dir"];

    //create an image object from the image in that path
    System.Drawing.Image img = System.Drawing.Image.FromFile(url);

    //Rotate the image in memory
    if (direction == "clockwise")
    {
        //Rotate clockwise
        img.RotateFlip(RotateFlipType.Rotate90FlipNone);
    } else if (direction == "anticlockwise") 
    {
        //Rotate anti-clockwise
        img.RotateFlip(RotateFlipType.Rotate90FlipXY);
    }

    //Delete the file so the new image can be saved
    System.IO.File.Delete(url);

    //save the image to the file
    img.Save(url);

    //release image file
    img.Dispose();
}
</script>
Run Code Online (Sandbox Code Playgroud)

Kar*_*son 6

由于您没有在发布的代码中显示实际的页面名称,我将假设tesing.aspx应该是testing.aspx,将您的URL更改为:

/testing/testing.aspx 
Run Code Online (Sandbox Code Playgroud)

更新1:

您正尝试通过<%语法使用内联嵌入式代码块,但您需要使用<script语法,因为您的逻辑跨越多行.

试试这个:

<script runat="server">
    using System; 

    protected string callRotate()
    ... Rest of your code here
</script>
Run Code Online (Sandbox Code Playgroud)

更新2:

嵌入式代码块不允许使用C#using语句,而是必须使用@ Importpage指令,如下所示:

<%@ Import Namespace="System" %>
Run Code Online (Sandbox Code Playgroud)

更新3:

ProcessStartInfo类是部分System.Diagnostics命名空间,因此添加以下的进口对于这一点,就像这样:

<%@ Import Namespace="System.Diagnostics" %>
Run Code Online (Sandbox Code Playgroud)

更新4:

您可以将文件名和参数组合在一起,如下所示:

Process execute = new Process();
execute.StartInfo.FileName = "ConsoleApplication1";
execute.StartInfo.Arguments = @"-log d:file.txt -c ""arg2"" -y ""arg3"" -z ""HOW?""";
execute.Start()
Run Code Online (Sandbox Code Playgroud)

注意:Arguments此处的值已组成,因为我不确定您在控制台应用程序中设置了哪些标记ConsoleApplication1.

更新5:

更改script块中的方法以处理ASP.NET Page_Load事件,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    string url = Request.QueryString["url"];
    string rotate_dir = Request.QueryString["dir"];
}
Run Code Online (Sandbox Code Playgroud)