【发布时间】:2016-06-23 02:24:28
【问题描述】:
我正在尝试实现我自己的动态路由器,我的计划是从我的数据库中提取路由并创建一组动态登录页面,我的问题是在将 context.RouteData 设置为我的新页面后我得到 404路线数据。
我只想在每次找到路线时重定向到我的 LandingPageController 和索引 IActionResult。
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using System.Collections.Generic;
using System.Linq;
namespace Myproject.Web.Main.Config
{
public class LandingPageRouter : IRouter
{
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
return null;
}
public Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
{
requestPath = requestPath.Substring(1);
}
var pagefound = GetPages().Any(x => x == requestPath);
if (pagefound)
{
//TODO: Handle querystrings
var routeData = new RouteData();
routeData.Values["controller"] = "LandingPage";
routeData.Values["action"] = "Index";
context.RouteData = routeData;
}
return Task.FromResult(0);
}
private IEnumerable<string> GetPages()
{
//TODO: pull from database
return new List<string> { "page-url-title", "another-dynamic-url" };
}
}
}
我查看了这个answer,但它似乎已经过时,上下文中的某些属性在 RC2 中甚至不再存在。
我错过了什么?
【问题讨论】:
-
请停止使用术语 MVC6。当/如果发布旧版 MVC 的新版本时,您只会散布混乱!以前的“ASP.NET 5 MVC6”现在被命名为“ASP.NET Core MVC 1.0”以明确它不是 ASP.NET MVC5 的下一个版本,而是一个全新的从头开始编写的框架。正确的标签也是
asp.net-core-mvc! -
看起来很有趣……您能提供更多信息吗?因此,如果像
/articles/cavaliers-win-nba-title这样的请求进入,您希望映射到/articles/10url,并由articles控制器处理并获取文章ID10,并且在生成链接时,您希望映射发生反过来说……我说的对吗? -
@KiranChalla 我想在根级别配置路由,例如 mydomain.com/name-of-the-article 和内部配置,并将这些路由映射到我的登陆页面控制器,它可以是登陆页面/Id 或登录页面/文章名称
-
@Tseng 是的,我知道这是一个全新的版本,它被重命名为 Core MVC,我只是使用这个名称,因为旧问题是这样写的,所以我的错,我会尽量记住它在我的下一个问题中,感谢您的提醒
标签: c# asp.net-core asp.net-mvc-routing asp.net-core-mvc