【问题标题】:Asp.net Routing not working properlyAsp.net 路由无法正常工作
【发布时间】:2015-02-23 17:48:51
【问题描述】:

我在 Webforms 应用程序的 Global.asax 中的 RegisterRoutes() 中编写了以下内容。 我有一个名为 CacheInfo.xml 的 xml 文件,它存储缓存页面的详细信息。默认页面在 xml 中有一个名为 EnDefaultPage 的条目,其中包含两个子节点。 子节点 LastUpdationTimeStamp 存储 Default 页面缓存的最后更新时间戳(unix 时间戳,以秒为单位)。

子节点ExpiryTimestamp存储缓存将过期的时间戳。因此,如果过期时间戳大于lastupdation时间戳,则将从缓存中加载文件(默认.html)。当前脚本正在运行,但它尽管正在执行定义路由的条件块,但没有路由到 Default.html。我找不到问题背后的确切原因。任何人都可以建议我修复。所有其他路由都运行良好。 我的代码附在下面。

ublic void RegisterRoutes(RouteCollection routes)
    {

        string relPath = "~/CacheInfo.xml";
        string absPath = Server.MapPath(relPath);


        XmlDocument xdoc = new XmlDocument();//xml doc used for xml parsing

        xdoc.Load(
            absPath
            );//loading XML in xml doc

        string nodeName="", nodeVal="",upStamp="0",expiry="0";
        int iUpStamp,iExpiry;
        upStamp = xdoc.GetElementsByTagName("EnDefaultPage")[0].SelectSingleNode("LastUpdationTimeStamp").InnerText;
        expiry =  xdoc.GetElementsByTagName("EnDefaultPage")[0].SelectSingleNode("ExpiryTimeStamp").InnerText;

        iUpStamp = Convert.ToInt32(upStamp);
        iExpiry = Convert.ToInt32(expiry);

        if(!String.IsNullOrEmpty(upStamp)) {
            if (iExpiry > iUpStamp)
            {
                System.Diagnostics.Debug.WriteLine("stamp is " + upStamp + " Expiry is " + expiry);
                //  routes.MapPageRoute("EnHome1", "", "~/en/DefaultCache.aspx");
                routes.MapPageRoute("EHome", "en/", "~/en/Default.html");
               //this routing is not working
            }
            else
            {
            }
        }

        routes.MapPageRoute("GArticle", "Gallery/{slug}", "~/Gallery/Article.aspx");


        routes.MapPageRoute("GCategory", "Gallery/Categories/{slug}", "~/Gallery/Categories.aspx");

        routes.MapPageRoute("GSlideShow", "Gallery/{slug}/{id}", "~/Gallery/SlideShow.aspx");

        routes.MapPageRoute("Article", "en/{slug}", "~/english/Article.aspx");


        routes.MapPageRoute("Category", "en/Categories/{slug}", "~/english/Categories.aspx");
        routes.MapPageRoute("enFeed", "en/category/english/{slug}/feed", "~/en/feed.aspx");

        routes.MapPageRoute("teFeed", "te/category/telugu/{slug}/feed", "~/te/feed.aspx");

         routes.MapPageRoute("Tags", "en/Tags/{tag}", "~/english/Tags.aspx");



         routes.MapPageRoute("tArticleHtml", "te/{slug}.html", "~/telugu/Article.aspx");

         routes.MapPageRoute("tArticle", "te/{slug}", "~/telugu/Article.aspx");


         routes.MapPageRoute("tCategoryHtml", "te/Categories/{slug}.html", "~/telugu/Categories.aspx");
         routes.MapPageRoute("tCategory", "te/Categories/{slug}", "~/telugu/Categories.aspx");

         routes.MapPageRoute("tTagsHtml", "te/Tags/{tag}.html", "~/telugu/Tags.aspx"); 

         routes.MapPageRoute("tTags", "te/Tags/{tag}", "~/telugu/Tags.aspx");


    }
}

【问题讨论】:

    标签: asp.net routing


    【解决方案1】:

    如果我正确阅读了您的意图,您希望用户在其访问权限到期时被引导到默认状态。如果是这样,你不能从这里到达那里。

    RegisterRoutes 用于在应用程序启动时注册路由。最后 3 个单词是关于为什么某些逻辑在应用程序稍后无法工作的线索。

    现在到您的 RegisterRoutes。这是逻辑。

    1. 获取到期日期
    2. 获取时间戳
    3. 如果过期日期大于上次更新,当应用程序启动时,将用户路由到默认值

    如果您真正的意思是“在启动时确定是否每次都将每个可能过期的页面路由到默认值”,那么您的逻辑是正确的并且它按预期工作。你问这个问题的事实表明这不是你的意图。您希望在逐个页面访问的基础上对页面访问做出决定。如果我是正确的,你不能在 RegisterRoutes 中做到这一点。您将不得不在别处实现“缓存逻辑”。

    我的建议是研究处理缓存的软件,而不是手动构建。

    【讨论】:

    • 谢谢...但是 routes.MapPageRoute("EHome", "en/", "~/en/Default.html"); 背后的原因是什么?不工作?任何解决方案
    • 这是你的意图吗? 1. 启动 Web 服务器 2. 确定所有当前过期的页面(在启动时)是否转到 default.htm。如果您希望逐案处理(它现在是否过期,而不是在应用程序启动时),那么您不能这样做。 RegisterRoutes 旨在设置路线的“字典”,可以这么说,在 START UP。它不用于运行时条件逻辑。从启动的角度来看,它很可能运行良好,但您需要运行时确定。如果您这样做,我建议您查看类似 OutputCache 之类的东西,它在 RegsiterRoutes 中没有使用。
    • 注意:如果您需要自定义缓存,您仍然无法在 RegisterRoutes 中为运行时实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2016-11-26
    • 2014-12-05
    • 2018-01-17
    • 2017-10-15
    相关资源
    最近更新 更多