[英]Re-direct image requests in RouteConfig file (ASP.NET MVC)?
我在ASP.NET MVC项目中使用jQuery插件时遇到问题,该项目在不存在的位置搜索图像。
我收到像这样的缺失图像错误:
GET http://localhost:51710/Home/img/chesspieces/wikipedia/bN.png 404 (Not Found)
但是我不知道为什么看起来要像这样在localhost/Home
内部搜索图像?
我想在内容文件夹中搜索图像(例如\\Content\\chessboard.js\\img
),而不是像localhost/Home
这样的视图/视图控制器。
为什么会这样,如何实现我想要的?
我的routeconfig
文件仍处于默认状态:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
编辑:
这是chessboard.js中的行,我可以在其中更改localhost/Home
之后的路线,但不能更改localhost/Home
本身:
cfg.pieceTheme = 'img/chesspieces/wikipedia/{piece}.png';
使用绝对路径:
cfg.pieceTheme = '/img/chesspieces/wikipedia/{piece}.png'; // note the '/'
有一个非常有用的网址扩展名,可以指向内容文件(例如图像)的适当位置:
@Url.Content
这样便可以从适当的内容目录中获取文件。
@Url.Content("~/Content/chessboard.js/img")
如果无法在此文件中使用Razor语法,则可以尝试以下操作:
cfg.pieceTheme = "~/Content/chessboard.js/img/chesspieces/wikipedia/{piece}.png';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.