[英]How can I configure a (.net6) WebApplication with a handler for all GET requests that don't have a MapGet?
I have a WebApplication running inside a console app with various MapGet handlers configured.我有一个 WebApplication 在配置了各种 MapGet 处理程序的控制台应用程序中运行。
var app = WebApplication.Create();
app.MapGet("/", GetHome);
/* Other MapGet calls and handlers removed for brevity. */
app.Run();
Because this would be replacing an established website, I would like to specify a custom handler for any GET request that doesn't have its own MapGet instead of the normal 404 response.因为这将替换已建立的网站,所以我想为没有自己的 MapGet 而不是正常的 404 响应的任何 GET 请求指定一个自定义处理程序。
Is there a way I can specify a GET handler of last resort?有没有办法可以指定最后的 GET 处理程序? (One that allows any number of slash-separated folders and any or no file extension on the end.)
(一个允许任意数量的斜线分隔的文件夹和任何或没有文件扩展名。)
With a bit of experimentation, this seemed to work, if more by accident than design.通过一些实验,这似乎是可行的,如果更多的是偶然而不是设计。
app.MapGet("/{**x}", MyHandler);
The double-asterisk appears to indicate multiple folders are allowed.双星号似乎表示允许多个文件夹。 The "x" is there because MapGet will only accept the string if the part has a name.
“x”在那里是因为 MapGet 仅在零件有名称时才接受字符串。 If
MyHandler
has an HttpContext
parameter, the URL is available in the context.Request.Path
property.如果
MyHandler
具有HttpContext
参数,则 URL 在context.Request.Path
属性中可用。
I hope someone has a better answer to this.我希望有人对此有更好的答案。 It feels there should be a Map call that simply says "All unmapped requests go here".
感觉应该有一个 Map 调用,简单地说“所有未映射的请求都到这里”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.