端点在 .net core 2.1 中不起作用,为什么?
end point not working in .netcore 2.1, why?
这是一个关于为什么会发生这种情况的非常简单的问题。
首先,我有一个 .net core 2.1 项目,我需要 3 个额外的端点,所以这是我的代码:
app.Map("/h1", handle1);
app.Map("/h1/h2", handle2);
app.Map("/h1/h3", handle3);
在配置方法中。 handle1、handl2 和 handle3 是在 localhost:port/h1、localhost:port/h1/h2 和 localhost:port/h1/h3.
上写入不同内容的自定义方法
但是这不起作用,因为我在 localhost:port/h1/h2 得到的结果与其他两个相同,所以 localhost:port/h1 是正确的,但 localhost:port/h1/h2 和 localhost:port/h1/h3 显示 localhost:port/h1 这是不正确的。
我已经尝试了一些东西,这是可行的:
app.Map("/h1", handle1);
app.Map("/h/h2", handle2);
app.Map("/h/h3", handle3);
问题是为什么?以及如何使 localhost:port/h1 和 localhost:port/h1/h2 和 localhost:port/h1/h3 工作?
更新:
我试过了,效果很好,但我不明白为什么
app.Map("/h1/h2", handle2);
app.Map("/h1/h3", handle3);
app.Map("/h1", handle1);
重要的是检查顺序。
假设您的 url 是:
url = "/h1/h2/somethingcool"
您的地图构建方式是:
[
"/h1"
"/h1/h2",
"/h1/h3"
]
所以当你的地图循环时会发生这种情况(伪代码):
for(map.entries() : entry) {
if(url.startsWith(entry.url) {
entry.doTheThing(url);
break;
}
}
所以当我们修补那里的值时:
url = "/h1/h2/somethingcool"
foreach value in [ "/h1", "/h1/h2", "/h1/h3" ] put it in entry
loop 1:
entry = "/h1"
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1")
do the route
abort looping
Loop is aborted
stopping processing other array items.
现在,如果我们采用您的其他有效地图,我们将获得预期的行为。
url = "/h1/h2/somethingcool"
foreach value in [ "/h1/h3", "/h1/h2", "/h1" ] put it in entry
loop 1:
entry = "/h1/h3"
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1/h3")
it did not match. Continue looping.
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1/h2")
do the route
abort looping
Loop is aborted
stopping processing other array items.
我希望这可以帮助您理解流程背后的逻辑。这一切都归结为 url 如果以它开头则匹配,而不是完全匹配。
这是一个关于为什么会发生这种情况的非常简单的问题。
首先,我有一个 .net core 2.1 项目,我需要 3 个额外的端点,所以这是我的代码:
app.Map("/h1", handle1);
app.Map("/h1/h2", handle2);
app.Map("/h1/h3", handle3);
在配置方法中。 handle1、handl2 和 handle3 是在 localhost:port/h1、localhost:port/h1/h2 和 localhost:port/h1/h3.
上写入不同内容的自定义方法但是这不起作用,因为我在 localhost:port/h1/h2 得到的结果与其他两个相同,所以 localhost:port/h1 是正确的,但 localhost:port/h1/h2 和 localhost:port/h1/h3 显示 localhost:port/h1 这是不正确的。
我已经尝试了一些东西,这是可行的:
app.Map("/h1", handle1);
app.Map("/h/h2", handle2);
app.Map("/h/h3", handle3);
问题是为什么?以及如何使 localhost:port/h1 和 localhost:port/h1/h2 和 localhost:port/h1/h3 工作?
更新:
我试过了,效果很好,但我不明白为什么
app.Map("/h1/h2", handle2);
app.Map("/h1/h3", handle3);
app.Map("/h1", handle1);
重要的是检查顺序。
假设您的 url 是:
url = "/h1/h2/somethingcool"
您的地图构建方式是:
[
"/h1"
"/h1/h2",
"/h1/h3"
]
所以当你的地图循环时会发生这种情况(伪代码):
for(map.entries() : entry) {
if(url.startsWith(entry.url) {
entry.doTheThing(url);
break;
}
}
所以当我们修补那里的值时:
url = "/h1/h2/somethingcool"
foreach value in [ "/h1", "/h1/h2", "/h1/h3" ] put it in entry
loop 1:
entry = "/h1"
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1")
do the route
abort looping
Loop is aborted
stopping processing other array items.
现在,如果我们采用您的其他有效地图,我们将获得预期的行为。
url = "/h1/h2/somethingcool"
foreach value in [ "/h1/h3", "/h1/h2", "/h1" ] put it in entry
loop 1:
entry = "/h1/h3"
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1/h3")
it did not match. Continue looping.
if([url]"/h1/h2/somethingcool" starts with [entry]"/h1/h2")
do the route
abort looping
Loop is aborted
stopping processing other array items.
我希望这可以帮助您理解流程背后的逻辑。这一切都归结为 url 如果以它开头则匹配,而不是完全匹配。