代码之家  ›  专栏  ›  技术社区  ›  twk

区域中ASPX文件中的Href路径

  •  0
  • twk  · 技术社区  · 16 年前

    我一直在测试ASP.NET MVC 2预览版2的新功能“一个项目中的区域”。 目前,我在从ASPX代码中链接到CSS和JS文件时遇到问题。

    当URL指向一个没有ID的URL时,一切正常:

    http://mysite.com/area/controller/action

    当URL包含参数时出现问题:

    http://mysite.com/admin/controller/action/id

    然后,页面无法从/content和/scripts中找到CSS和JS文件。

    我认为这个问题与路由有关。我设置了标准路由规则,例如:

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            AreaRegistration.RegisterAllAreas();
    
            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );
    

    在该区域的路由配置中:

            public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" }
                );
        }
    

    ASPX文件中的示例资源HREF:

        <link href="../../Content/datatables.css" rel="stylesheet" type="text/css" />
    

    有人能提出解决坏资源问题的建议吗?

    2 回复  |  直到 16 年前
        1
  •  1
  •   bobince    16 年前

    当你使用URL路由时,你不知道在你的URL中会有多少个/path/parts。所以你根本不能使用路径相关的URL:你不知道你需要多少段。

    相反,使用根URL:

    <link href="/Content/datatables.css" rel="stylesheet" type="text/css" />
    

    如果您的应用程序可以安装在非根URL上(例如,在您的 area _s,您必须将该区域名作为根URL的一部分输出。(大概是用 AreaRegistrationContext.AreaName )

        2
  •  0
  •   takepara    16 年前

    设置runat=“server”属性怎么样?

    <head runat="server">
      <link href="~/Content/datatables.css" rel="stylesheet" type="text/css" runat="server" />
      ...
    
    推荐文章