mvc4 actionlink图像

时间:2012-10-19 14:33:54

标签: c# asp.net-mvc-4

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

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

5 个答案:

答案 0 :(得分:30)

图像始终相对于当前CSS的位置。

如果您使用的是内联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;" 
    }
)

或者如果你决定定义一个CSS类:

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

您已在.mylink中定义了~/content/Site.css规则:

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

答案 1 :(得分:12)

您可以采取以下最佳方式

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

完美的工作

答案 2 :(得分:4)

您可以使用此

@Html.ActionLink("linkName","action","Controller")

这工作正常,它与Sep 29, 2015 8:59:15 AM org.apache.catalina.startup.Catalina start SEVERE: The required Server component failed to start so Tomcat is unable to start. org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.startup.Catalina.start(Catalina.java:691) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:322) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:456) Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 7 more Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]] at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154) at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 9 more Caused by: org.apache.catalina.LifecycleException: A child container failed during start at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131) at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 11 more

类似

答案 3 :(得分:1)

我编辑了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\" />"))

如果您希望它没有默认边框,请将其添加到您的css

img {border: 0px;}

答案 4 :(得分:0)

我发现当需要高级<stdarg.h>标记自定义(例如锚点内的其他标记)时,最好自己动手。

我使用UrlHelper.GenerateUrl方法生成网址,然后手动创建<cstdarg>标记并使用生成的网址。