假设我有一堆渲染文本部分的 Controller 。它们可以通过浏览器的 url 访问。但其中一些只是其他人的组合。我想要的是请求几个文本并将其连接起来。为什么?
- 它们需要时间来构建,但很可能已经缓存
- 不要发明我自己的缓存并使用 ASP.NET
可能我可以 System.Net.WebClient.DownloadString("https://myserver/path")
但是效率很低。
下面的@derloopkat 亲切地问我有关代码示例的问题
[ApiController]
[Route("foo")]
public abstract class FooController: ControllerBase
{
[HttpGet]
public string Get()
{
return "foo";
}
}
[ApiController]
[Route("bar")]
public abstract class BarController: ControllerBase
{
[HttpGet]
public string Get()
{
return "bar";
}
}
[ApiController]
[Route("bar_plus_foo")]
public abstract class CompositeController: ControllerBase
{
[HttpGet]
public string Get()
{
// TODO: how to fetch result of FooController and BarController
// to return concatenation?
}
}
最佳答案
我们通常不会这样在controller中写业务逻辑。 您可以创建一个服务并在该服务中隐藏您的业务逻辑。代码将是这样的:
public class BarService
{
public string GenerateBar(string param)
{
return "bar";
//Or any business logic that you have
}
}
例如,您可以搜索洋葱架构并在 github 中查看示例 顺便说一下,不要让 Controller 抽象。 希望这有帮助
关于c# - 如何从 asp.net core web 应用程序向自己发出请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695819/