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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3391510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:24:08  来源:igfitidea点击:

ASP MVC Friendly URL's and Relative Path Images

asp.net-mvcrelative-path

提问by doug

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

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

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

所以,我在主控制器中有一个类别视图,它接受一个 categoryKey 值来获取页面的内容。

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

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

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

在我的 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
  );
}

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".

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

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

回答by marcind

Use the Url.Contentmethod when generating a path to your image.

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

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

or if using the WebForms view engine:

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

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

回答by Pavel Morshenyuk

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:

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

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

回答by LawMan

This works too.

这也有效。

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

回答by Pavel Nazarov

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.

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