ASP.NET MVC路由 [英] ASP.NET MVC routing

查看:140
本文介绍了ASP.NET MVC路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到现在为止,我已经能够逃脱使用的ASP.NET MVC附带的默认路由。不幸的是,现在我正在拓展至更复杂的路线,我挣扎,我的包裹围绕如何得到这个工作的头。

我试图让一个简单的例子是有路径/用户/用户名{} /项目映射到用户控制器的项目的功能。谁能告诉我我在做什么毛病我这里路由?

  routes.MapRoute(UserItems,用户/用户名{} /项目,
                      新{控制器=用户,行动=项目});

和我的aspx页面

  Html.ActionLink(项目,UserItems,新的用户名{= 1})


解决方案

去了MVC preVIEW 4 code我在我的面前了Html.ActionLink()您所使用的过载是这个

 公共字符串的ActionLink(字符串LINKTEXT,串actionName,对象值);

请注意第二个参数是如何的 actionName 的不是的 routeName

因此​​,请尝试:

  Html.ActionLink(项目,项目,新的用户名{= 1})

另外,请尝试:

 < A HREF =<%= Url.RouteUrl(UserItems,新的{用户ID = 1})%>>项目< / A>

Up until now I've been able to get away with using the default routing that came with ASP.NET MVC. Unfortunately, now that I'm branching out into more complex routes, I'm struggling to wrap my head around how to get this to work.

A simple example I'm trying to get is to have the path /User/{UserID}/Items to map to the User controller's Items function. Can anyone tell me what I'm doing wrong with my routing here?

routes.MapRoute("UserItems", "User/{UserID}/Items", 
                      new {controller = "User", action = "Items"});

And on my aspx page

Html.ActionLink("Items", "UserItems", new { UserID = 1 })

解决方案

Going by the MVC Preview 4 code I have in front of me the overload for Html.ActionLink() you are using is this one:

public string ActionLink(string linkText, string actionName, object values);

Note how the second parameter is the actionName not the routeName.

As such, try:

Html.ActionLink("Items", "Items", new { UserID = 1 })

Alternatively, try:

<a href="<%=Url.RouteUrl("UserItems", new { UserId = 1 })%>">Items</a>

这篇关于ASP.NET MVC路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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