【发布时间】:2018-04-06 20:07:54
【问题描述】:
在我的家庭控制器中,我有两个视图,Index 和 About。
我正在尝试配置对example.com/about 的请求以访问About 页面。
在我的 Startup.cs 文件中,我有以下 MapRoute
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
"Id",
"{id}",
new { controller = "Home", action = "About" });
});
这允许将对example.com/about 的请求传递到About 操作中。我的问题是它还捕获了正斜杠之后的所有其他请求。例如,对example.com/foobarr 的请求也会触发About 操作。
如何更改 MapRoute,以便对 example.com/about 的请求到达 About 操作,但忽略其他任何内容。
【问题讨论】:
-
About操作是否真的采取了Id? -
不,About 操作不使用 id。
-
@TidyDev 那么为什么要在路由模板中指定
id?
标签: c# asp.net asp.net-core-mvc asp.net-core-routing