ASP.NET MVC Html.ActionLink结果网址 - 编码方式方式、网址、结果、NET

2023-09-03 03:39:13 作者:橘味美人

我创造的动作的量MVC控制器。

 公众的ActionResult DoSmth1(字符串标记)
公众的ActionResult DoAnother2(字符串标记)
 

当我必须调用ActionLink的..

  = Html.ActionLink< SomeController>(X => x.DoSmth(item.property),item.property)
= Html.ActionLink< AnotherController>(X => x.DoAnother(item.property),item.property)
 

...它生成了我不同的网址:

  /有/ DoSmth /字符串值
/另一个/ DoAnother?属性=字符串值
 
ASP.NET MVC 2 模型验证

在哪里设置它建立一个网址的方式吗?我ALR没有想法......((

OK,一个得到了一些waylight: - 如果属性名是在路由模式中使用的相同 - 例如控制器,动作和id - MVC的意志的总是使用路由生成器(/ C / A / ID)

这有助于一点点(让 - 命名的参数ID尽可能))

但整体的问题仍然是有效的...

       

必须被命名为相同的路线标记

  

没错 - 我第一次有这样的想法。

可是 - 现在我只有默认路由({控制器} / {行动} / {ID}),但仍然有URL与斜线财产......这是pretty的怪异

还有一个骗子的方法 - 以创建符合给定的控制器,它是一个precise路由的参数名称 - 是,似乎这将是最终的答案 - 但我还是不希望这样做(( 解决方案

您不会显示你的路线,但你几乎可以肯定打在这个例子中不同的路线。该参数你的行动必须为生成的URL到路由令牌的ActionLink的拉姆达形式匹配被命名为相同的路线令牌才能。任何不匹配路由令牌将被追加作为查询字符串参数,与你的第二个网址。眼看查询字符串参数是有力的证据,你通过隐含的名称(在这种情况下,属性)不匹配的路由标记。既然你得到不同的结果有同样的名字,我由此断定你打不同的路线。顺便说一句,我建议用RouteLink代替ActionLink的建设环节,让你可以肯定的是哪条路线,你会匹配。

I create an amount of actions in MVC controllers.

public ActionResult DoSmth1(string token)
public ActionResult DoAnother2(string token)

And when I have to call ActionLink..

=Html.ActionLink<SomeController>( x=> x.DoSmth(item.property), item.property)
=Html.ActionLink<AnotherController>( x=> x.DoAnother(item.property), item.property)

...it generates me different URLs:

/Some/DoSmth/stringvalue
/Another/DoAnother?property=stringvalue

Where to set the way it builds an URL? I am alr have no ideas...((

OK, a got some waylight: - if the property names are the same that used in routing schema - eg controller,action and id - the MVC will always use the route builder (/c/a/id).

That helps a little (so - name the parameter "id" as possible))

But the overall problem is still valid...

must be named the same as the token in the route

Exactly - i first had that idea.

But - now i have only default route ({controller}/{action}/{id}) but still have the URL with "property" in slashes... This is pretty weird.

there's also a cheater way - to create a precise route that match a given controller with it's parameter names - is seems that will be the ultimate answer - but i still don't want to do this ((

解决方案

You don't show your routes, but you're almost certainly hitting different routes in this example. The argument to your action must be named the same as the token in the route in order for the generated URL to match the route token with the lambda form of ActionLink. Anything which does not match a routing token will be appended as a query string parameter, as with your second URL. Seeing the query string parameter is strong evidence that the name you passed implicitly ("property" in this case) does not match a route token. Since you get different results with the same token name, I thereby conclude you are hitting different routes. Incidentally, I recommend building links with RouteLink instead of ActionLink, so that you can be certain of which route you will match.