基本上我有一个CMS后端,我使用ASP.NET MVC建立,现在我正在移动到前端网站,并需要能够从我的cms数据库加载页面,基于输入的路线。
因此,如果用户输入domain.com/students/information,MVC将在pages表中查看是否存在具有匹配学生/信息的固定链接的页面,如果存在,它将重定向到页面控制器,然后从数据库加载页面数据,并将其返回到视图以供显示。
到目前为止,我已经尝试了一个catch all路由,但它只适用于两个URL段,即So /students/information,而不是/students/information/fall。我在网上找不到任何关于如何做到这一点的东西,所以我想在我找到并打开源码ASP.NET MVC cms并剖析代码之前,我想我应该在这里问一下。
这是我到目前为止的路由配置,但我觉得有一种更好的方法。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default route to handle core pages
routes.MapRoute(null,"{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new { controller = "Index" }
);
// CMS route to handle routing to the PageController to check the database for the route.
var db = new MvcCMS.Models.MvcCMSContext();
//var page = db.CMSPages.Where(p => p.Permalink == )
routes.MapRoute(
null,
"{*.}",
new { controller = "Page", action = "Index" }
);
}
如果有人能给我指出正确的方向,我将如何从数据库加载CMS页面,最多三个URL段,并仍然能够加载核心页面,有一个控制器和动作预定义。
发布于 2019-07-04 03:59:50
我使用更简单的方法,不需要任何自定义路由器处理。只需创建一个处理几个可选参数的单个/全局控制器,然后按您喜欢的方式处理这些参数:
//Route all traffic through this controller with the base URL being the domain
[Route("")]
[ApiController]
public class ValuesController : ControllerBase
{
//GET api/values
[HttpGet("{a1?}/{a2?}/{a3?}/{a4?}/{a5?}")]
public ActionResult<IEnumerable<string>> Get(string a1 = "", string a2 = "", string a3 = "", string a4 = "", string a5 = "")
{
//Custom logic processing each of the route values
return new string[] { a1, a2, a3, a4, a5 };
}
}
domain.com/test1/test2/test3上的示例输出
["test1","test2","test3","",""]
https://stackoverflow.com/questions/16026441
复制相似问题