java - 在 ASP.NET Web API 中创建输入参数并使用 java web 服务功能

标签 java c# asp.net web-services

我创建了一个具有附加功能的java web 服务。我还创建了一个 ASP.NET Web API,它调用 java web 服务并显示结果。假设我输入了 http://localhost:8080/addition/9/6 作为 URL,其中包含 Java Web 服务函数应添加的输入参数。我得到的输出数据为 {"firstNumber":9,"secondNumber":6,"sum":15}。当我运行 ASP.NET Web API 时,我将被重定向到 http://localhost:55223/ 并且我将获得输出数据 {"firstNumber":9,"secondNumber “:6,”总和“:15}

现在,我想做的是,当我运行 ASP.NET Web API 时,我应该能够在 ASP.NET Web API 的 URL 中输入参数(http://localhost: 55223/addition/9/6),而不是直接显示来自Java Web服务的结果,我想在我的API中使用Java Web服务的功能来计算输入参数的总和。有谁知道我该怎么做?我应该在代码中进行哪些更改?

这是我的代码:

ASP.NET Web API 代码

RestfulClient.cs

public class RestfulClient
{
    private static HttpClient client;
    private static string BASE_URL = "http://localhost:8080/";

    static RestfulClient()
    {
        client = new HttpClient();
        client.BaseAddress = new Uri(BASE_URL);
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
    }

    public async Task<string> addition(int firstNumber, int secondNumber)
    {
        try
        {
            var endpoint = string.Format("addition/{0}/{1}", firstNumber, secondNumber);
            var response = await client.GetAsync(endpoint);
            return await response.Content.ReadAsStringAsync();
        }
        catch (Exception e)
        {
            HttpContext.Current.Server.Transfer("ErrorPage.html");
        }
        return null;
    }
}

ApiController.cs

public class ApiController : Controller
{
    private RestfulClient restfulClient = new RestfulClient();

    public async Task<ActionResult> Index()
    {
        int firstNumber = 9;
        int secondNumber = 6;
        var result = await restfulClient.addition(firstNumber, secondNumber);
        return Content(result);
    }
}

Java Web 服务代码

AdditionController.java

@RestController
public class AdditionController {

private static final String template = " %s";
private static int getSum;

@RequestMapping("/addition/{param1}/{param2}")
@ResponseBody 

public Addition addition 
            (@PathVariable("param1") int firstNumber,@PathVariable("param2") int secondNumber) {
return new Addition(
        (String.format(template, firstNumber)), String.format(template, secondNumber));
  }
}   

请有人帮助我,提前非常感谢。

最佳答案

what i want to do is, when i run my ASP.NET Web API, i should be able to input parameters in the URL of the ASP.NET Web API (http://localhost:55223/addition/9/6)

Web API 使用许多约定,如果你玩得好,那么一切都会很好地工作。您需要做的第一件事是重命名您的 Controller ,如下所示:

public class AdditionController : ApiController
{
    public async Task<IHttpActionResult> Get(int firstNumber, int secondNumber)
    { 
        var result = await restfulClient.addition(firstNumber, secondNumber);
        return Ok(result);
    }
}

您会注意到上面代码中的一些内容:

  1. Controller 称为AdditionController。当您在 url 中输入addition 时,路由引擎将查找名为Addition + 单词Controller 的 Controller 。
  2. 它继承ApiController而不是Controller
  3. 它有一个 Get 操作。如果您发出 GET 请求,API 路由引擎将搜索名为 Get 或以 Get 开头的操作。因此它会找到这个 Action 。
  4. 该操作正在返回 IHttpActionResult。看我的回答here为什么。
  5. 它使用名为 Ok 的扩展方法返回 HTTP 状态代码 200。这是为了遵循良好的 Restful 准则和 HTTP 准则。

您可以这样调用上面的内容:

http://localhost:55223/addition?firstNumber=1&secondNumber=6
<小时/>

如果您希望能够像这样调用它:

http://localhost:55223/addition/9/6

然后您需要对WebApiConfig类进行一些更改。将其添加到 DefaultApi 代码之前的代码中:

config.Routes.MapHttpRoute(
    name: "AdditionApi",
    routeTemplate: "api/addition/{firstNumber}/{secondNumber}", 
    defaults: new { action = "Get", controller = "Addition" }
);

上面我们告诉路由引擎:嘿路由引擎,如果 url 包含单词 api/addition 后跟一个变量和另一个变量,那么使用 Get Addition Controller *的方法。

关于java - 在 ASP.NET Web API 中创建输入参数并使用 java web 服务功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007805/

相关文章:

java - 如何使用scriptlet提高从oracle 10g到jsp页面的数据传输速度

java - 返回对象的 HashSet.contains()

Java - 如何在类中使用现有的枚举

c# - 如何配置应用程序以在具有高 DPI 设置(例如 150%)的机器上正确运行?

c# - 如何使用 asp.net web 表单从代码后面访问文本框、更新面板内的标签

java - 停止长时间等待的线程

c# - 如何将数据集绑定(bind)到具有显示和值成员的下拉列表/组合框

c# - 自定义错误处理 Asp.Net

通用标记的 ASP.NET 虚拟目录

c# - 是否可以在多个母版页中调用一个网络表单?