动态添加到 RouteTable 不起作用

发布于 2024-12-29 19:30:43 字数 1685 浏览 0 评论 0原文

我目前正在为应用程序编写新闻文章方面,并将新路由(用于新文章)添加到我的 RouteTable 中,这似乎添加得很好,但路由无法访问?

我使用的代码如下:

var routes = RouteTable.Routes;
using (routes.GetWriteLock())
{
    var url = contentHelper.GetPageUrl(page);
    routes.MapRoute(page.Id.ToString(), url, new { controller = "Cms", action = "Index", id = url }, new[] { "Web.Controllers.CmsController" });
}

正如我之前所说,新的 Url 已添加到 RouteTable.Routes 中,但我无法访问该页面。重新启动后,它会被 Global.asax 中的 RegisterRoutes 拾取并正常工作。

您能对此提供的任何说明都会很棒,因为我希望在不强制重新启动应用程序的情况下实现此目的

编辑

这是来自我的global.asax的RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("LogOff", "LogOff", new { controller = "Account", action = "LogOff" });

        routes.MapRoute(
                "News", // Route name
                "News/",// URL with parameters
                new { controller = "NewsPage", action = "Index" }, // Parameter defaults
                new[] { "Web.Controllers.NewsController" }
            );

        //register all content pages
        var urls = new ContentPageHelper().GetAllUrls();
        foreach (var url in urls)
        {
            routes.MapRoute(url.Key.ToString(), url.Value, new { controller = "Cms", action = "Index", id = url.Key }, new[] { "Web.Controllers.CmsController" });
        }

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",// URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

I am currently writing a news article aspect to an application and I adding the new route (for the new article) to my RouteTable, which appears to add fine, but the routes are unreachable?

The code I am using is as follows:

var routes = RouteTable.Routes;
using (routes.GetWriteLock())
{
    var url = contentHelper.GetPageUrl(page);
    routes.MapRoute(page.Id.ToString(), url, new { controller = "Cms", action = "Index", id = url }, new[] { "Web.Controllers.CmsController" });
}

The new Url, as I previously said, is added to the RouteTable.Routes but I cant get to the page. After a restart it is picked up by RegisterRoutes in the Global.asax and works fine.

Any light that you can shed on this would be great, as I want to achieve this without forcing an app restart

EDIT

This is the RegisterRoutes from my global.asax

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute("LogOff", "LogOff", new { controller = "Account", action = "LogOff" });

        routes.MapRoute(
                "News", // Route name
                "News/",// URL with parameters
                new { controller = "NewsPage", action = "Index" }, // Parameter defaults
                new[] { "Web.Controllers.NewsController" }
            );

        //register all content pages
        var urls = new ContentPageHelper().GetAllUrls();
        foreach (var url in urls)
        {
            routes.MapRoute(url.Key.ToString(), url.Value, new { controller = "Cms", action = "Index", id = url.Key }, new[] { "Web.Controllers.CmsController" });
        }

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",// URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2025-01-05 19:30:43

问题在于调用 RouteTable.MapRoute 将路由添加到表的末尾。您的默认路由通常是最后添加到表中的路由,但由于您在构建路由表后动态调用 RouteTable.MapRoute,因此默认路由在到达您的路由之前就会被命中。新添加的路线。

我对这个问题的解决方案如下。

    public static void AddContentRoute(this RouteCollection routes, Data.UrlMap map, bool needsLock = false)
    {
        var route = new Route(map.Url, new MvcRouteHandler());
        route.Defaults = new RouteValueDictionary(new
        {
            controller = "Content",
            action = "Display",
            id = map.Id,
            type = map.Type
        });

        if (needsLock)
        {
            using (routes.GetWriteLock())
            {
                routes.Insert(0, route);
            }
        }
        else
        {
            routes.Insert(0, route);
        }
    }

这本质上与 RouteTable.MapRoute 的作用相同,但将路由插入到表的顶部而不是末尾。

如果您有其他路由需要位于表的顶部,您可能需要修改此解决方案。

The issue is that calling RouteTable.MapRoute adds the route to the end of the table. Your default route is generally the last route to be added to the table, but since you are dynamically calling RouteTable.MapRoute after you have already built your route table, the default route is being hit before it reaches your newly added route.

My solution to this problem was as follows.

    public static void AddContentRoute(this RouteCollection routes, Data.UrlMap map, bool needsLock = false)
    {
        var route = new Route(map.Url, new MvcRouteHandler());
        route.Defaults = new RouteValueDictionary(new
        {
            controller = "Content",
            action = "Display",
            id = map.Id,
            type = map.Type
        });

        if (needsLock)
        {
            using (routes.GetWriteLock())
            {
                routes.Insert(0, route);
            }
        }
        else
        {
            routes.Insert(0, route);
        }
    }

This essentially does what RouteTable.MapRoute does, but inserts the route to the top of the table instead of the end.

If you have other routes that need to sit at the top of the table you may want to modify this solution.

眼眸印温柔 2025-01-05 19:30:43

为什么要动态创建路线?当应用程序自行重新启动应用程序时,这是否会违背目的,所有这些路由都需要重新创建才能工作

为什么不创建一个仅处理对您的 cms 的所有请求的路由?

routes.MapRoute("CmsPage", "{page}", new { controller = "Cms", action = "Index"}, new [] { "Web.Controllers.CmsController" });

如果您这样做是为了不与应用程序中的其他路由/控制器发生冲突,那么您始终可以在所有 cms 生成的页面前添加 page.我正在为我现在创建的 CMS 网站执行此操作。

routes.MapRoute("CmsPage", "pages/{page}", new { controller = "Cms", action = "Index"}, new [] { "Web.Controllers.CmsController" });

您在控制器中的操作将与此类似

public ActionResult Index(string page) 
{
    // logic to load page
    return View();
}

Why would you create a route on the fly? Wouldn't this defeat the purpose when the application does an app restart on it's own, all of those routes would need to be recreated for them to work

Why not create a route that just handles all of the request to your cms?

routes.MapRoute("CmsPage", "{page}", new { controller = "Cms", action = "Index"}, new [] { "Web.Controllers.CmsController" });

if you're doing it so it's not colliding with other routes/controllers in your applications, you could always prefix all cms generated pages with pages. I'm doing this for a CMS site that I'm creating now.

routes.MapRoute("CmsPage", "pages/{page}", new { controller = "Cms", action = "Index"}, new [] { "Web.Controllers.CmsController" });

Your action in your controller would be similar to this

public ActionResult Index(string page) 
{
    // logic to load page
    return View();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文