英文:
ASP.NET Core Web API : accept any route, any method, any verb
问题
是的,这是可能的。
英文:
I need to create a mock service that accepts any route, any HTTP verb, etc..
[GET or POST or Whatever] http://[ip]/anyaction
Is it possible?
答案1
得分: 1
你可以创建自己的中间件,简单地回复所有请求:
public class AcceptAllMiddleware
{
private readonly RequestDelegate _next;
public AcceptAllMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
context.Response.StatusCode = StatusCodes.Status200OK;
await context.Response.WriteAsync("我接受任何请求。");
await context.Response.CompleteAsync();
}
}
然后从Startup类中删除中间件管道中的所有内容,添加你的中间件:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<AcceptAllMiddleware>();
}
英文:
You can create your own middleware that simply replies all requests:
public class AcceptAllMiddleware
{
private readonly RequestDelegate _next;
public AcceptAllMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
context.Response.StatusCode = StatusCodes.Status200OK;
await context.Response.WriteAsync("I accept anything and everything.");
await context.Response.CompleteAsync();
}
}
Then remove everything from the middleware pipeline on the Startup class and add your middleware:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMiddleware<AcceptAllMiddleware>();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论