简体   繁体   English

Asp.net MVC的不同路由导致相对寻址问题

[英]Asp.net MVC different routes cause relative addressing issues

I'm having an issue where im reusing partial controls in my asp.net mvc project. 我在asp.net mvc项目中重用部分控件时遇到问题。 The situation is that we the website can be accessed from 2 different paths: eg 情况是我们可以通过两种不同的途径访问我们的网站:

  1. http://localhost/mywebsite
  2. http://localhost/mywebsite/app/otherapp

These two address share controls and on those shared controls there are some img html tags with relative addressing throughout. 这两个地址共享控件,在这些共享控件上,有一些img html标记,它们始终具有相对寻址。

ie

This relative image address works fine for address web address 1 but fails for web address 2. To get it working for web address 2 i need to change it to 此相对图像地址在地址网址1上工作正常,但在网址2上却失败。要使其在网址2下工作,我需要将其更改为

I've got these throughout the entire system and it would be a nightmare to go through and switch them at runtime. 我在整个系统中都拥有这些,并且在运行时进行切换是一个噩梦。

Wondering if there is someway to cover this situation without all the hassle? 想知道是否有某种方式可以轻松解决这种情况?

您应该在“ src”中使用“〜”从主目录开始:

<img alt="Ok" src='~/img/buttons/Ok.png' />
<img alt="img1" src='/img/buttons/Ok.png' />

or use Url.Action() method. 或使用Url.Action()方法。

<img alt="img1" src="@Url.Action('')"/>

use Url.Action method overload of your choice. 使用您选择的Url.Action方法重载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM