C# 重构Angular服务以调用函数App而不是WebApi
我正在重构Angular应用程序的一部分,以便与Azure函数应用程序通信,而不是与经典的WebApi通信 WebApi非常标准,如下所示C# 重构Angular服务以调用函数App而不是WebApi,c#,angular,azure-functions,C#,Angular,Azure Functions,我正在重构Angular应用程序的一部分,以便与Azure函数应用程序通信,而不是与经典的WebApi通信 WebApi非常标准,如下所示 [Route("[controller]/[action]")] public class WebApiController : BaseController { [HttpGet] public async Task<IActionResult> GetSomething(int id) {
[Route("[controller]/[action]")]
public class WebApiController : BaseController
{
[HttpGet]
public async Task<IActionResult> GetSomething(int id)
{
return Ok();
}
}
[路由(“[controller]/[action]”)
公共类WebApicController:BaseController
{
[HttpGet]
公共异步任务GetSomething(int-id)
{
返回Ok();
}
}
并通过如下方式调用,生成如下请求:http://localhost:4000/WebApi/GetSomething?id=10
public getSomething(id: number) : Observable<Array<Data>> {
return this.http.get<Array<Data>>(ApiEndpoints.products.getSomething, { params: { id: id.toString() }});
}
public getSomething(id:number):可观察{
返回这个.http.get(apidemps.products.getSomething,{params:{id:id.toString()}});
}
有了新的函数,应用程序后端Api看起来像这样
public class FunctionAppController : FunctionService
{
[FunctionName("GetSomething")]
public async Task<IActionResult> GetSomething(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "GetSomething/{id}")] HttpRequest req, ILogger log, long id)
{
return Ok();
}
}
公共类functionppcontroller:FunctionService
{
[FunctionName(“GetSomething”)]
公共异步任务GetSomething(
[HttpTrigger(AuthorizationLevel.Anonymous,“get”,Route=“GetSomething/{id}”)]HttpRequest请求,ILogger日志,长id)
{
返回Ok();
}
}
当调用函数应用程序时,我收到一个404,因为它期望一个如下所示的请求:http://localhost:4000/WebApi/GetSomething/10
我可以通过所有的Angular应用程序和更新服务,如下所示,但这将是一个相当粗糙的解决方案
public getBlockHeadersFromTop(id: number) : Observable<Array<Data>> {
return this.http.get<Array<Data>>(ApiEndpoints.products.getSomething + "/" + id.toString());
}
public getBlockHeadersFromTop(id:number):可观察{
返回此.http.get(apidendpoints.products.getSomething+“/”+id.toString());
}
因此,我的问题是,是否有任何简单而优雅的解决方案可以在Angular services中格式化url结构而不更改其原始实现,或者将后端更改为以查询字符串形式接收url?对于此问题,我不清楚你为什么用
GetSomething/{id}
在函数中设置Route
如果您想要函数,则需要一个请求,如:http://localhost:4000/WebApi/GetSomething?id=10
。您只需将路线
更改为GetSomething
。然后当您使用urlhttp://localhost:4000/WebApi/GetSomething?id=10
,您可以使用字符串id=req.Query[“id”]
获取函数代码中的查询参数
嗨,我不太清楚你的最后一句话,因为英语不好。如果我误解了你的要求,请告诉我。