MVC3路由 - href添加了额外的路由

时间:2012-09-20 13:04:47

标签: .net asp.net-mvc-3 routing

我有一个html助手:actionLink在'a href'中生成'img'

所以这个:

Html.ActionImage"Create", "Premises", "~/Images/Views/Premises/LicensableActivitiesButton.png", "Licensable Activities", new { id = "licensableActivitiesImg" })</div>

生成这个:

<a id="licensableActivitiesImg" href="/Users/Premises/Create"><img src="/Images/Views/Premises/LicensableActivitiesButton.png" alt="Licensable Activities">

问题是它将'/ Users /'附加到href的开头,我不知道为什么。这个追加也在我的开始形式中发生:

using (@Html.BeginForm("Create", "Premises", FormMethod.Post, new { id = "premisesForm" })) 

给出:

<form id="premisesForm" method="post" action="/Users/Premises/Create">

我的路线很简单:

       routes.MapRoute(
             "",
             "Premises/Premises/Create",
             new { controller = "Premises", action = "Create" }
             );


        routes.MapRoute("Default", // Route name                
                        "{controller}/{action}/{id}", // URL with parameters                
                        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults            
                       );

没有使用区域。

HTML帮助程序代码:

    public static MvcHtmlString ActionImage(this HtmlHelper html, string action, string 
     Controller, string imagePath, string altText, object htmlAttributes, object routeValues)
    {
        var url = new UrlHelper(html.ViewContext.RequestContext);

        // build the <img> tag 
        var imgBuilder = new TagBuilder("img");
        imgBuilder.MergeAttribute("src", url.Content(imagePath));
        imgBuilder.MergeAttribute("alt", altText);
        string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

        // build the <a> tag 
        var anchorBuilder = new TagBuilder("a");

        anchorBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues));
        anchorBuilder.InnerHtml = imgHtml; // include the <img> tag inside 
        string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

        return MvcHtmlString.Create(anchorHtml);
    }

任何想法/建议都表示赞赏。

1 个答案:

答案 0 :(得分:0)

确定, 经过几个小时的挫折,我发现断点没有受到打击。

原来正在加载一个旧的程序集。

检查构建设置:项目 - &gt;属性 - &gt;构建选项卡

构建输出路径设置为:/ bin / x86 / Debug

将其更改为:/ bin

并且点击断点并且href显示正确的路径。

呼!