区域中 aspx 文件中的 href 路径

发布于 2024-08-06 11:55:24 字数 1492 浏览 8 评论 0原文

我正在不断测试 ASP.NET MVC 2 Preview 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" />

任何人都可以提出解决方案来解决错误资源 href 的问题吗?

I am continually testing the new feature of ASP.NET MVC 2 Preview 2 called: "Areas within one project".
Currently I have a problem with linking to css and js files from within aspx code.

When the url points to an url without the id, everything works fine:

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

The problem appears when the url contains the parameter:

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

then the page cannot find css and js files from /content and /scripts.

I think the problem is related to routing. I have standard routing rules set, eg:

        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
        );

and in the area's route config:

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

An example resource href in aspx file:

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

Can anyone suggest a solution to resolve the problem of bad resource href?

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

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

发布评论

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

评论(2

嘿哥们儿 2024-08-13 11:55:24

当您使用 URL 路由时,您事先并不知道 URL 中会有多少/多少/路径/部分。因此,您根本无法使用路径相对 URL:您不知道需要多少个“..”段。

相反,请使用根 URL:

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

如果您的应用程序可以安装在非根 URL 上(例如,在您的某个区域下),则必须将该区域名称作为根 URL 的一部分输出。(大概是这样)这是使用 AreaRegistrationContext.AreaName 获得的。)

When you are using URL routing, you don't know how/many/path/parts there are going to be in your URL in advance. So you can't use path-relative URLs at all: you don't know how many ‘..’ segments you are going to need.

Instead, use rooted URLs:

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

If your application can be mounted on a non-root URL (eg. under one of your area​s, you must output that area name as part of the rooted URL. (Presumably this is obtained using AreaRegistrationContext.AreaName.)

歌枕肩 2024-08-13 11:55:24

怎么样,设置 runat="server" 属性。

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

How about this, set runat="server" attribute.

<head runat="server">
  <link href="~/Content/datatables.css" rel="stylesheet" type="text/css" runat="server" />
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文