简体   繁体   English

如何为没有 MapGet 的所有 GET 请求配置具有处理程序的 (.net6) WebApplication?

[英]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.

相关问题 我如何使用 WebApplication MapGet 处理程序将图像作为 HTTP 响应发送? - How can I, using a WebApplication MapGet handler, send an image as an HTTP response? 如何连接到ASP.NET Core的WebApplication.db数据库? - How can I connect to ASP.NET Core's WebApplication.db database? .net核心-中间件不处理请求 - .net core - Middleware don't handle requests .net 6 MVC Razor 站点-登录部分链接打不开页面,路由问题? 我怎样才能解决这个问题? - .net 6 MVC Razor site - loginPartial links don't open the page, routing issue? How can I fix this? 如何减小 .Net6 中 winforms 可执行文件的大小? - How to reduce size of winforms executable in .Net6? 如何在 dotnet 6 Minimal Api app.MapGet 函数上获取 aws lambda APIGatewayProxyRequest 参数 - How to get aws lambda APIGatewayProxyRequest parameter on dotnet 6 Minimal Api app.MapGet function 我如何才能对 .net 核心中的所有 api 做出通用 api 响应? - How can i have the generic api response to all of my api in .net core? 为什么在扩展方法中我无法访问 .net 核心中的一些其他 IServiceCollection 扩展? - Why in extension method I don't have access to some other IServiceCollection extensions in .net Core? 我没有合适的 arguments 用于 AddModulesAsync (C# Discord.Net) - I don't have the right arguments for AddModulesAsync (C# Discord.Net) 如何在Windows 7计算机上运行.Net Core Web应用程序? - How to run a .Net Core webapplication on a Windows 7 machine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM