ASP MVC 友好 URL 和相对路径图像

发布于 2024-09-12 04:35:47 字数 1486 浏览 2 评论 0原文

我有一个 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,它也会砍掉第一个 ../,因为它正在进行一些验证。

I have an ASP.NET MVC page, where I am trying to show friendly URL's.

So, I have a Category View in the Home Controller, that accepts a categoryKey value to get the content of the page.

For instance: http://localhost/Home/Category/Bikes gets the bike content.

in my Global.asax.cs, i have the following to handle this:

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

This works just fine, and it gets the content, however, I am getting the content from a Content Management system, for easy editing. When you add an image on the content management, it adds the image with a relative path:

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

Now, if i goto "http://localhost/Home/Category", and that image tag is on the base page, it will pull up the image. However, if i goto "http://localhost/Home/Category/", or add the actual category of "/Home/Category/Bikes"/, the image doesn't show up. The properties of the image is pointing to "http://localhost/Home/AdminUploadContent/bikes.gif".

Is there anything I can put in my Global.aspx.cs file to handle the relative path? Even if i manually edit the content management to add ../../AdminUploadContent/bikes.gif, it is chopping off the first ../, since it is doing some validation.

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

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

发布评论

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

评论(5

不气馁 2024-09-19 04:35:47

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

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

或者如果使用 WebForms 视图引擎:

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

Use the Url.Content method when generating a path to your image.

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

or if using the WebForms view engine:

<img src="<%= Url.Content("~/AdminUploadContent/bikes.gif") %>" />
无尽的现实 2024-09-19 04:35:47

可以使用相对路径“~/”来引用页面当前所在的虚拟目录。尝试将 runat="server" 属性添加到“img”标签中,并在“src”属性中添加“~”符号:

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

You can use the relative path " ~/ " to refer to the current virtual directory where the page is located. Try to add runat="server" attribute to your "img" tag and "~" sign in "src" attribute:

<img runat="server" src="~/AdminUploadContent/bikes.gif" alt="Bikes" />
情感失落者 2024-09-19 04:35:47

这也有效。

<img src="@Url.Content("~/Images/yourimage.png")"/>

This works too.

<img src="@Url.Content("~/Images/yourimage.png")"/>
绮烟 2024-09-19 04:35:47

例如,您可以实现一个自定义 http 模块,它将重写从相对 ../../img.png 到处理 Referer http 标头所需的路径。

You can implement a custom http module that will rewrite path from the relative ../../img.png to needed handling the Referer http header, for instance.

戈亓 2024-09-19 04:35:47

我必须将 / 添加到 src 属性

Before:

<div><img alt='' src='" & image_url & "' style='vertical-align: top; height: 40px'/><div class='auto-style1' style='padding: 10px'></div>

After:

<div><img alt='' src='/" & image_url & "' style='vertical-align: top; height: 40px'/><div class='auto-style1' style='padding: 10px'></div>

其中 image_url 是我的页面代码隐藏类的函数。

I had to just Add / to the src attribute

Before:

<div><img alt='' src='" & image_url & "' style='vertical-align: top; height: 40px'/><div class='auto-style1' style='padding: 10px'></div>

After:

<div><img alt='' src='/" & image_url & "' style='vertical-align: top; height: 40px'/><div class='auto-style1' style='padding: 10px'></div>

Where image_url is a Function of my page code-behind class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文