加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在MVC中覆盖@ url.content?

发布时间:2020-12-16 01:51:04 所属栏目:百科 来源:网络整理
导读:我有一个MVC应用程序.完成网站后,我需要更改@ url.content行为. 所以我需要在我的所有网站应用程序中覆盖@ url.content.我该怎么做? script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"/scriptscript src="@Url.Content("~/Scripts/ui/jquery.ui.core
我有一个MVC应用程序.完成网站后,我需要更改@ url.content行为.

所以我需要在我的所有网站应用程序中覆盖@ url.content.我该怎么做?

<script src="@Url.Content("~/Scripts/jquery-1.7.1.js")"></script>
<script src="@Url.Content("~/Scripts/ui/jquery.ui.core.js")"></script>
<script src="@Url.Content("~/Scripts/ui/jquery.ui.widget.js")"></script>
<script src="@Url.Content("~/Scripts/ui/jquery.ui.tabs.js")"></script>
<script src="@Url.Content("~/Scripts/ui/jquery.ui.accordion.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.nivo.slider.js")"></script>
<script src="@Url.Content("~/Scripts/jwplayer.js")"></script>

解决方法

MHF,

正如我在上面的评论中提到的,我觉得你应该创建一个定制的html.image()帮助器,而不是试图覆盖url.content()帮助器,因为你的问题与图像有关,而不是url.content ()本身.这是我如何处理这个问题:

public static partial class HtmlHelperExtensions
{
    public static MvcHtmlString Image(this HtmlHelper helper,string url,object htmlAttributes)
    {
        return Image(helper,url,null,htmlAttributes);
    }
    public static MvcHtmlString Image(this HtmlHelper helper,string altText,object htmlAttributes)
    {
        TagBuilder builder = new TagBuilder("image");

        var path = url.Split('?');
        string pathExtra = "";

        // NB - you'd make your test for the existence of the image here
        // and create it if it didn't exist,then return the path to 
        // the newly created image - for better or for worse!! :)

        if (path.Length > 1)
        {
            pathExtra = "?" + path[1];
        }
        builder.Attributes.Add("src",VirtualPathUtility.ToAbsolute(path[0]) + pathExtra);
        builder.Attributes.Add("alt",altText);
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
}

用途:

@Html.Image("~/content/images/ajax-error.gif",new{@class="error_new"})

现在,上面纯粹是一个旧的mvc项目的“升力”,添加了一个注释,以暗示你可能会做什么.我没有以任何方式测试过这个,所以请注意:)

祝好运

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读