ASP MVC友好的URL和相对路径图片 [英] ASP MVC Friendly URL's and Relative Path Images
问题描述
我有一个ASP.NET MVC页面,在这里我想显示友好的URL的。
I have an ASP.NET MVC page, where I am trying to show friendly URL's.
所以,我在家庭控制器,它接受一个categoryKey值获得页面内容的分类视图。
So, I have a Category View in the Home Controller, that accepts a categoryKey value to get the content of the page.
例如:的http://本地主机/首页/分类/自行车得到自行车含量
在我的Global.asax.cs中,我必须处理这个以下内容:
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" />
现在,如果我转到HTTP://本地主机/首页/目录,并认为图片标签是基本页面上,这将拉动图像。但是,如果我转到HTTP://本地主机/首页/分类/,或加上/首页/分类/自行车/实际范畴,图像显示不出来。所述图像的属性指向的http://localhost/Home/AdminUploadContent/bikes.gif
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".
有什么我可以把我的Global.aspx.cs文件来处理的相对路径?即使我手动编辑的内容管理添加../../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.
推荐答案
使用的 Url.Content 方法来生成图像的路径时。
Use the Url.Content method when generating a path to your image.
<img src="@Url.Content("~/AdminUploadContent/bikes.gif")" />
或者如果使用的WebForms视图引擎:
or if using the WebForms view engine:
<img src="<%= Url.Content("~/AdminUploadContent/bikes.gif") %>" />
这篇关于ASP MVC友好的URL和相对路径图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!