如何在Angular get请求中将字符串值发送到后端

时间:2019-05-10 19:49:46

标签: angular asp.net-core

我希望获得一个核心域名,网址中会附加股票名称。这是代码:

[Route("api/[controller]")]
[ApiController]
public class StockController : ControllerBase
{
    private static readonly Random generator = new Random();

    // GET api/stock/aapl
    [HttpGet("{r}")]
    public ActionResult<float[]> Get(
        string r,

我不确定Angular get中的格式。这就是我所拥有的:

  getStock(name: string) {
// url should be api/stock/aapl
console.log("this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});", this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name}))

return this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});
}

后端和前端都进行编译。 console.log语句将其发送到控制台,格式不正确:

console.log("this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name});", this.http.get<Cagr[]>(this.apiURL + '/api/stock' + {name}))

我应该使用哪种格式?

1 个答案:

答案 0 :(得分:1)

您似乎缺少斜杠。如果您使用占位符而不是内联串联,那么您的代码将更具可读性

const url = `${this.apiURL}/api/stock/${name}`;
return this.http.get<Cagr[]>(url);