gpt4 book ai didi

asp.net-mvc - ASP MVC 友好 URL 和相对路径图像

转载 作者:行者123 更新时间:2023-12-02 12:37:00 24 4
gpt4 key购买 nike

我有一个 ASP.NET MVC 页面,我试图在其中显示友好的 URL。

因此,我在主 Controller 中有一个类别 View ,它接受categoryKey 值来获取页面的内容。

例如:http://localhost/Home/Category/Bikes获取自行车内容。

在我的 Global.asax.cs 中,我有以下方法来处理此问题:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
"Category",
"{controller}/{action}/{categoryKey}",
new { controller = "Home", action = "Category", categoryKey = "" });

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}

这工作得很好,并且它获取内容,但是,我从内容管理系统获取内容,以便于编辑。当您在内容管理上添加图像时,它会添加带有相对路径的图像:

<img src="../AdminUploadContent/bikes.gif" alt="Bikes" />

现在,如果我转到“http://localhost/Home/Category”,并且该图像标签位于基本页面上,它将拉出图像。但是,如果我转到“http://localhost/Home/Category/”,或添加实际类别“/Home/Category/Bikes”/,则图像不会显示。图像的属性指向“http://localhost/Home/AdminUploadContent/bikes.gif ”。

我可以在 Global.aspx.cs 文件中放入任何内容来处理相对路径吗?即使我手动编辑内容管理以添加 ../../AdminUploadContent/bikes.gif,它也会砍掉第一个 ../,因为它正在进行一些验证。

最佳答案

使用Url.Content生成图像路径时的方法。

<img src="@Url.Content("~/AdminUploadContent/bikes.gif")" />

或者如果使用 WebForms View 引擎:

<img src="<%= Url.Content("~/AdminUploadContent/bikes.gif") %>" />

关于asp.net-mvc - ASP MVC 友好 URL 和相对路径图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391510/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com