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

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

所以,我在家庭控制器中有一个类别视图,它接受一个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视图引擎:

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....