C# mvc4 动作链接图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12976488/
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-08-10 01:10:51  来源:igfitidea点击:

mvc4 actionlink image

c#asp.net-mvc-4

提问by Gina Marano

I am trying to replace the infamous "your logo here" in _Layout.cshtml for the ASP.NET MVC4 Web Application. 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.

我正在尝试为 ASP.NET MVC4 Web 应用程序替换 _Layout.cshtml 中臭名昭著的“您的徽标”。以下工作(在图像中显示)适用于主页(主页视图),但不适用于联系人视图(没有图像,但操作有效)。我需要它在开发环境和生产环境中都能工作。

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

采纳答案by Darin Dimitrov

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 助手:

@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 .mylinkrule in ~/content/Site.css:

您在以下位置定义了.mylink规则~/content/Site.css

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

回答by Dilip0165

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

哪个完美地工作

回答by Muflix

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;}

回答by Amit Ghute

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

回答by Illidan

I found that when required high level of <a>tag customization (e.g. 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。