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,它也会砍掉第一个 ../,因为它正在进行一些验证。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 Url.Content 方法生成图像的路径。
或者如果使用 WebForms 视图引擎:
Use the Url.Content method when generating a path to your image.
or if using the WebForms view engine:
可以使用相对路径“~/”来引用页面当前所在的虚拟目录。尝试将 runat="server" 属性添加到“img”标签中,并在“src”属性中添加“~”符号:
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:
这也有效。
This works too.
例如,您可以实现一个自定义 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.
我必须将 / 添加到 src 属性
Before:
After:
其中 image_url 是我的页面代码隐藏类的函数。
I had to just Add / to the src attribute
Before:
After:
Where image_url is a Function of my page code-behind class.