MapRoute 到 Home 控制器中的特定操作
MapRoute to specific action in Home controller
在我的家庭控制器中,我有两个视图,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
操作,但忽略其他任何内容。
在controller内部配置路由,在方法上面指定属性不是更好吗,例如:
[HttpGet("Home/About")]
自定义属性用于关于路由的操作方法。在这种情况下,处理特定路线会更容易。
在我的家庭控制器中,我有两个视图,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
操作,但忽略其他任何内容。
在controller内部配置路由,在方法上面指定属性不是更好吗,例如:
[HttpGet("Home/About")]
自定义属性用于关于路由的操作方法。在这种情况下,处理特定路线会更容易。