【问题标题】:Calculating sum of URL parameters (numbers) using function from dll file使用 dll 文件中的函数计算 URL 参数(数字)的总和
【发布时间】:2017-11-07 09:30:49
【问题描述】:

我创建了一个执行添加功能的 dll 文件。现在,我正在创建一个 ASP.NET API,假设使用 dll 中创建的函数来执行添加。我希望这样当我运行 ASP.NET API 时,用户输入他们想要在 URL 中添加的两个数字作为参数,例如https://localhost:12345/api/addition/9/6 并且会有类似的输出数据(在 JSON 中){第一个数字是:9,第二个数字是:6,总和是:15}。

我已经设置了参数,以便用户可以输入数字。我创建了一个类来调用 dll。但是,我不确定应该在控制器类中放置什么,如何使用 dll 中的函数计算用户输入的数字以及如何显示输出。

这是我的代码:

Addition.cs

namespace NewAdditionAPI.Models
{
  using ClassLibraryDll;
  public class Addition
  {
    static int num1;
    static int num2;
    int sum = MathClass.Add(num1, num2);
  }

public string addition (int num1, int num2)
  {
    //What do i have to code here
  }
}

DllAdditionController.cs

public class Temp
{
    public string num1 { get; set; }
    public string num2 { get; set; }
    public string sum { get; set; }
}

public class DllAdditionController : ApiController
{
    private Addition addition = new Addition();

    public string GET(int num1, int num2)
    {
        //What do i have to code here
    }
}

WebApiConfig.cs

public static void Register(HttpConfiguration config)
{
        // Web API configuration and services
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DllAdditionApi",
            routeTemplate: "api/dlladdition/{num1}/{num2}",
            defaults: new { action = "Get", controller = "DllAddition" }
        );
}

dll 文件

public static class MathClass
{
    //method for Addition
    public static int Add(int num1, int num2)
    {
        return num1 + num2;
    }
}

有人请帮助我。非常感谢您。

【问题讨论】:

    标签: c# asp.net dll


    【解决方案1】:
    public string GET(int num1, int num2)
    {
         //What do i have to code here
    
         int result = your.dll.MathClass.Add(num1, num2);
    }
    

    【讨论】:

    • 在 WEBAPI 的情况下,即使您使用 num1 而不是 firstNumber(从路由设置),它是否也能正常工作?
    猜你喜欢
    • 2012-10-27
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    相关资源
    最近更新 更多