Url.Action(动作,控制器,routeValues​​)的URL标识增加了一倍 [英] Url.Action(action,controller,routeValues) doubling up ID in URL

查看:166
本文介绍了Url.Action(动作,控制器,routeValues​​)的URL标识增加了一倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code(从后#1所:动作图像MVC3剃刀)在HTML扩展帮助建立一个动作链接。我Url.Action()方法返回了在路由的routeValues​​既以及追加到像这样的URL网址:

  /建议/列表/提交?ID =提交

当我想要的只是

  /建议/列表?ID =提交

为什么它要做到这一点有什么建议?

更新

从我的Global.asax 文件的路由规则。这正是为什么它这样做,但为什么它变得成倍上涨仍是一个谜给我。

  routes.MapRoute(_
    ProposalsList,_
    提案/列表/ {}状态,_
    随着新{.controller =建议,.action =列表,.STATUS =待定} _
    )

更新

下面是我的调用方法,我添加的方法定义如下code。

  @ Html.ActionImage(建议,列表,新增功能{.ID = Model.StatusFilter},〜/图片/+ Model.ImageFile,计数 ,32,32,Model.ProposalsCount.ToString ++ Model.StatusFilter +建议(S))

下面是我的code:

 <扩展()> _
    公共职能ActionImage(BYVAL HTML作为的HtmlHelper,BYVAL控制器作为字符串,BYVAL动作作为字符串,BYVAL routeValues​​作为对象,BYVAL的ImagePath作为字符串,BYVAL AL​​T作为字符串,BYVAL宽度为整数,BYVAL高度为整数,BYVAL文本作为字符串)作为MvcHtmlString
            昏暗的URL =新UrlHelper(html.ViewContext.RequestContext)
            昏暗imgHtml作为字符串
            昏暗anchorHtml作为字符串
            昏暗的imgbuilder =新TagBuilder(img目录)            imgbuilder.MergeAttribute(src用户,url.Content(的ImagePath))
            imgbuilder.MergeAttribute(Alt键,ALT)
            imgbuilder.MergeAttribute(宽宽)
            imgbuilder.MergeAttribute(高度,高度)
            imgHtml = imgbuilder.ToString(TagRenderMode.SelfClosing)            昏暗anchorBuilder =新TagBuilder(一)
            anchorBuilder.MergeAttribute(的href,url.Action(动作,控制器,routeValues​​))
            anchorBuilder.InnerHtml = imgHtml +< BR />中+文字
            anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal)            返回MvcHtmlString.Create(anchorHtml)
    结束功能


解决方案

当你传递routeValues​​到 url.action 方法会使用的值来覆盖当前定义那些(在当前页面请求上下文的)。

因此​​,当目前的状态为提交,你不复位,在您通过新routeValues​​,那么它仍然会使用它。

不过既然你传递一个id以及它补充说,太..

您需要通过新增功能{.ID = Model.StatusFilter,.STATUS =没什么}

层次是( 通过 http://forums.asp.net /t/1328683.aspx 的)


    在Url.Action调用中指定
  1. 值,然后

  2. 在请求上下文当前页面中指定的,然后

  3. 为路由的默认值。

I am using the following code (taken from a Stackoverflow post: Action Image MVC3 Razor ) in an HTML extension helper to build an action link. My Url.Action() method is returning a url that has the routeValues both in the route as well as appended onto the url like so:

/Proposals/List/Tabled?id=Tabled

when what I want is just

/Proposals/List?id=Tabled

Any suggestions about why it wants to do this?

Update:

A route rule from my Global.asax file. That must be why it's doing it, but why it's getting doubled up is still a mystery to me.

routes.MapRoute( _
    "ProposalsList", _
    "Proposals/List/{status}", _
    New With {.controller = "Proposals", .action = "List", .status = "Pending"} _
    )

Update:

Here is my call to the method, and I added the method definition to the code below.

@Html.ActionImage("Proposals", "List", New With {.id = Model.StatusFilter}, "~/images/" + Model.ImageFile, "Count", 32, 32, Model.ProposalsCount.ToString + " " + Model.StatusFilter + " Proposal(s)")

Here is my code:

    <Extension()> _
    Public Function ActionImage(ByVal html As HtmlHelper, ByVal controller As String, ByVal action As String, ByVal routeValues As Object, ByVal imagePath As String, ByVal alt As String, ByVal width As Integer, ByVal height As Integer, ByVal text As String) As MvcHtmlString                
            Dim url = New UrlHelper(html.ViewContext.RequestContext)
            Dim imgHtml As String
            Dim anchorHtml As String
            Dim imgbuilder = New TagBuilder("img")

            imgbuilder.MergeAttribute("src", url.Content(imagePath))
            imgbuilder.MergeAttribute("alt", alt)
            imgbuilder.MergeAttribute("width", width)
            imgbuilder.MergeAttribute("height", height)
            imgHtml = imgbuilder.ToString(TagRenderMode.SelfClosing)

            Dim anchorBuilder = New TagBuilder("a")
            anchorBuilder.MergeAttribute("href", url.Action(action, controller, routeValues))
            anchorBuilder.InnerHtml = imgHtml + "<br/>" + text
            anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal)

            Return MvcHtmlString.Create(anchorHtml)
    End Function

解决方案

When you pass routeValues to the url.action method it will use the values to override the currently defined ones (in the request context for the current page).

So when the current status is Tabled and you do not reset that in the new routeValues you pass, then it will still use it..

But since you pass an id as well it adds that too..

You need to pass New With {.id = Model.StatusFilter, .status = nothing}

the hierarchy is (from http://forums.asp.net/t/1328683.aspx)

  1. Values specified in the Url.Action call, then
  2. Values specified in the request context for the current page, then
  3. Default values for the route.

这篇关于Url.Action(动作,控制器,routeValues​​)的URL标识增加了一倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆