Asp MVC 3:动态解析视图中的相关资源
我有一个 javascript 应用程序,它在视图 (index.cshtml) 中运行。
问题:
问题是所有相对路径都是相对于当前url,这在简单的html webapp中是可以的,但在asp mvc中不行。 js 应用程序不必关心它是在普通 html 文件中还是通过 asp mvc 页面提供。
即 http://www.domain.com/
问题:
有没有办法将静态文件(图像,...,甚至可能是 js 文件)路由到内容文件夹,例如“~/Content/controller/action/
任何帮助表示赞赏!
LG warappa
PS:我知道 Url.Content() 但这不适合这里。
I have a javascript application that runs in a view (index.cshtml).
Problem:
The problem is all relative paths are relative to the current url, which would be ok in a simple html webapp but not in asp mvc. The js-app shouldn't have to bother whether it's served in a normal html file or via a asp mvc page.
I.e. http://www.domain.com/<controller>/<action>/ contains a script test.js. This script loads an external xml file searching relative to it ie. "data/data.xml". The resulting url reads http://www.domain.com/<controller>/<action>/data/data.xml. This isn't found.
Question:
Is there a way to route static files (images,..., maybe even js files) to the content folder like "~/Content/controller/action/<pathToFile>/"?
Any help appreciated!
Lg
warappa
PS: I know about Url.Content() but that doesn't fit here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该解决方案不需要映射 - 只需要标头中的一个简单的 html 标签:
Lg
warappa
编辑
某些浏览器需要绝对 URL - 示例已更新。
The solution doesn't require mapping - just a simple html tag in the header:
Lg
warappa
EDIT
Some browsers need an absolute url - sample updated.
您可以使用绝对 URL 地址来访问静态资源:
或者
这样,无论您是否在
/{Controller}/{Action} 中加载脚本,您都将始终获得相对于页面基地址的相同资源/{View}
、{Area}/{Controller}/{Action}/{View}
、自定义路由甚至静态脚本 html 页面中。或者也许您正在寻找的是 css 文件的使用,因为 CSS 的
url('')
解析相对于 CSS 文件位置的地址。您只需要导入一个包含所有资源(图像?)文件路径的 CSS 文件。然后脚本可以引用不同的类名,因此根本不知道位置。这就是像 jQuery UI 这样的库所做的事情。但这同样需要相对于 CSS 文档的固定文件夹结构。In you can use absolute URL addresses to access you static resources:
or
This way you will allways get the same resources relative to the page base address, no matter if you load the script in a
/{Controller}/{Action}/{View}
,{Area}/{Controller}/{Action}/{View}
, a custom route or even in a static script html page.Or perhaps what you're looking for is the use of css files, since CSS's
url('<path>')
resolves the addresses relative to the CSS file's location. You would just need to import the one CSS file that had all the resource (image?) file paths. Then the scripts could reference the distinct class names, thus not being location aware at all. This is what libraries like jQuery UI do. But again this would require a fixed folder structure relative to the CSS document.