简体   繁体   English

mvc4 actionlink图像

[英]mvc4 actionlink image

I am trying to replace the infamous "your logo here" in _Layout.cshtml for the ASP.NET MVC4 Web Application. 我试图在_Layout.cshtml中替换ASP.NET MVC4 Web应用程序中臭名昭着的“您的徽标”。 The following works (works as in the image is displayed) for the main page (Home view) but not on the contact view (no image but the action works). 主页面(主页视图)显示以下工作(在图像中显示),但在联系人视图上不显示(没有图像,但操作有效)。 I need this to work both in the development environment as well as the production environment. 我需要它在开发环境和生产环境中都能工作。

< p class="site-title">@Html.ActionLink(" ", "Index", "Home", new
  {
  style = "background: url('./Images/login_sm.bmp') no-repeat center right;
          display:block; height:84px; width:264px;"
  })
</ p>

Images are always relative to the location of the current CSS. 图像始终相对于当前CSS的位置。

If you are using inline CSS you should use url helpers: 如果您使用的是内联CSS,则应使用url helpers:

@Html.ActionLink(
    " ", 
    "Index", 
    "Home", 
    null , 
    new { 
        style = "background: url('" + Url.Content("~/images/login_sm.bmp") + "') no-repeat center right; display:block; height:84px; width:264px;" 
    }
)

or if you decide to define a CSS class: 或者如果您决定定义CSS类:

@Html.ActionLink(
    " ", 
    "Index", 
    "Home",  
    null , 
    new { 
        @class = "mylink" 
    }
)

where you have defined the .mylink rule in ~/content/Site.css : 你在~/content/Site.css定义.mylink规则的地方:

.mylink {
    background: url('../images/login_sm.bmp') no-repeat center right; 
    display: block; 
    height: 84px; 
    width: 264px;
}

The best way you can do as below 您可以采取以下最佳方式

@Html.Raw(@Html.ActionLink("[replacetext]", "Index", "Home").ToHtmlString().Replace("[replacetext]", "<img src=\"/assets/img/logo.png\" ... />"))

which perfectly working 完美的工作

You can use this 你可以用它

<a href='@Url.Action("action", "Controller")'>
<img src='@Url.Content("~/images/imageName.png")' /></a>

this is working fine and it works similar to @Html.ActionLink("linkName","action","Controller") 这工作正常,它的工作方式类似于@Html.ActionLink("linkName","action","Controller")

I edited Dilip0165 solution a little 我稍微编辑了Dilip0165解决方案

@Html.Raw(@Html.ActionLink("[replacetext]", "Index", "Home").ToHtmlString().Replace("[replacetext]", "<img src=\"" + @Url.Content("~/Content/assets/img/logo.png") + "\" alt=\"Link description\" title=\"Link description\" />"))

If you want it without default border, add this to your css 如果您想要它没有默认边框,请将其添加到您的CSS

img {border: 0px;}

I found that when required high level of <a> tag customization (eg other tags inside anchor), it is better to do it yourself. 我发现当需要高级别的<a>标签自定义(例如锚内的其他标签)时,最好自己动手。

I was using UrlHelper.GenerateUrl method for generating URL, and then I manually created <a> tag and used generated URL. 我使用UrlHelper.GenerateUrl方法生成URL,然后我手动创建<a>标签并使用生成的URL。

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

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