自定义路由未命中 asp.net 核心 mvc?
Custom route is not hitting in asp.net core mvc?
我有示例 .net 核心 mvc 应用程序,正在测试 SEO 友好 url 创建,
这是 posted Here
我已经创建了 post 中的所有代码。
我的测试控制器动作如下
public IActionResult Index(int id, string titl)
{
//string titl = "";
var viewModel = new FriendlyUrlViewModel() { Id = 1, Name = "Detail home view." };
string friendlyTitle = FriendlyUrlHelper.GetFriendlyTitle(viewModel.Name);
// Compare the title with the friendly title.
if (!string.Equals(friendlyTitle, titl, StringComparison.Ordinal))
{
return RedirectToRoutePermanent("post_detail", new { id = viewModel.Id, title = friendlyTitle });
}
return View(viewModel);
}
并且我已经在 startup.cs
中创建了自定义路线
app.UseMvc(routes =>
{
routes.MapRoute(
name: "post_detail",
template: "FriendlyUrl/index/{id}/{title}"
).MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
当我导航到
https://xxxxx:2222/FriendlyUrl/index/2/dfdsfdsfds
浏览器什么都不显示,
我认为问题出在自定义路由上,但我找不到它,
有人可以帮忙吗?
谢谢。
您不必添加:
routes.MapRoute(
name: "post_detail",
template: "FriendlyUrl/index/{id}/{title}")
这是全局 'controller/action' 映射规则。
相反,要将指定路径映射到操作,请考虑添加如下属性:
[Route("FriendlyUrl/index/{id}/{title}")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}
常见问题解答
如何使用路由名进行重定向?
只需为您的路线添加名称即可。像这样:
[Route("FriendlyUrl/index/{id}/{title}", Name = "post_detail")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}
要从另一个控制器或动作路由到此动作,请使用以下命令调用它:
// in action
return RedirectToRoute("post_detail");
// or
return RedirectToRoutePermanent("post_detail");
// I guess you don't have to lose the `Id`:
return RedirectToRoutePermanent("post_detail", new { id = 5 });
如何允许接收空标题
只需在路由模板中添加一个?
。像这样:
[Route("FriendlyUrl/index/{id}/{title?}", Name = "post_detail")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}
我有示例 .net 核心 mvc 应用程序,正在测试 SEO 友好 url 创建, 这是 posted Here
我已经创建了 post 中的所有代码。
我的测试控制器动作如下
public IActionResult Index(int id, string titl)
{
//string titl = "";
var viewModel = new FriendlyUrlViewModel() { Id = 1, Name = "Detail home view." };
string friendlyTitle = FriendlyUrlHelper.GetFriendlyTitle(viewModel.Name);
// Compare the title with the friendly title.
if (!string.Equals(friendlyTitle, titl, StringComparison.Ordinal))
{
return RedirectToRoutePermanent("post_detail", new { id = viewModel.Id, title = friendlyTitle });
}
return View(viewModel);
}
并且我已经在 startup.cs
app.UseMvc(routes =>
{
routes.MapRoute(
name: "post_detail",
template: "FriendlyUrl/index/{id}/{title}"
).MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
当我导航到
https://xxxxx:2222/FriendlyUrl/index/2/dfdsfdsfds
浏览器什么都不显示, 我认为问题出在自定义路由上,但我找不到它, 有人可以帮忙吗?
谢谢。
您不必添加:
routes.MapRoute(
name: "post_detail",
template: "FriendlyUrl/index/{id}/{title}")
这是全局 'controller/action' 映射规则。
相反,要将指定路径映射到操作,请考虑添加如下属性:
[Route("FriendlyUrl/index/{id}/{title}")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}
常见问题解答
如何使用路由名进行重定向?
只需为您的路线添加名称即可。像这样:
[Route("FriendlyUrl/index/{id}/{title}", Name = "post_detail")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}
要从另一个控制器或动作路由到此动作,请使用以下命令调用它:
// in action
return RedirectToRoute("post_detail");
// or
return RedirectToRoutePermanent("post_detail");
// I guess you don't have to lose the `Id`:
return RedirectToRoutePermanent("post_detail", new { id = 5 });
如何允许接收空标题
只需在路由模板中添加一个?
。像这样:
[Route("FriendlyUrl/index/{id}/{title?}", Name = "post_detail")]
public IActionResult Index([FromRoute]int id, [FromRoute]string title)
{
}