I just updated a WebForms/ASP.NET MVC 3 hybrid application to use mvc3 and the razor view engine. Everything seemed to be working fine however, now when I use Html.ActionLink()
my links do not get resolved correctly or something?
我刚刚更新了一个WebForms/ASP。NET MVC 3混合应用程序使用mvc3和razor视图引擎。但是,现在当我使用Html.ActionLink()时,我的链接没有得到正确的解决或什么?
For Example:
例如:
@Html.ActionLink("Create New", "Create")
generates an anchor tag like this:
生成一个这样的锚标记:
<a href="/?action=Create&controller=Network">Create New</a>
instead of what I would expect:
而不是我期望的:
<a href="/Network/Create">Create New</a>
Here is the controller action method:
这里是控制器动作方法:
public class NetworkController : Controller {
public ActionResult Create() {
return View(new Network());
}
}
Any suggestions would be great. Thank you.
任何建议都很好。谢谢你!
3 个解决方案
#1
1
As others have said, there's definitely something up with your route config.
正如其他人所说,您的路由配置肯定有问题。
The routing engine does two things; it resolves a URL to a set of routevalues (which is what's happening when you type /Network/Create in your browser and it goes to the correction action) and it resolves a set of routevalues to a URL (which is what happens when you do URL generation like in an actionlink). The routes are tested in order, and the first one that declares a match wins, which means you don't always have the same result both directions if you haven't set them up right.
路由引擎做两件事;它将URL解析为一组routevalues(这是您在浏览器中键入/Network/Create时发生的情况,并执行修正操作),并将一组routevalues解析为一个URL(这是在actionlink中进行URL生成时发生的情况)。路线是按顺序测试的,第一个宣布比赛获胜的路线,这意味着如果你没有正确设置它们,你不会总是在两个方向上都有相同的结果。
Recommend doing route testing w/ the MVCContrib route helpers, which may help you narrow down your problem. Also, it's generally a bad practice (or at least unreliable) to rely on the "ambient" route values, as @Chad Moran suggested, if you choose oneof the more explicit overloads of ActionLink you may hve better results as well.
建议做路径测试w/ mvc悔过路径助手,这可以帮助你缩小你的问题。另外,像@Chad Moran所建议的那样,依赖“环境”路径值通常是不好的做法(或者至少是不可靠的),如果您选择一个更显式的超负载ActionLink,您可能也会得到更好的结果。
#2
0
Have you made sure everything is set up correctly with routing? This seems to indicate that no route was found where controller
and action
were sufficient routes.
你确定所有的东西都正确设置了吗?这似乎表明,如果控制器和操作是足够的路由,则没有找到任何路由。
#3
0
Since you're not supplying the Controller parameter of the method it's based on context. Is this call inside of another controller's context?
因为你没有提供方法的控制器参数它是基于上下文的。这个调用是在另一个控制器的上下文中吗?
Try this...
试试这个…
@Html.ActionLink("Create New", "Create", "Network")
@Html。ActionLink(“创建新”、“创造”、“网络”)
#1
1
As others have said, there's definitely something up with your route config.
正如其他人所说,您的路由配置肯定有问题。
The routing engine does two things; it resolves a URL to a set of routevalues (which is what's happening when you type /Network/Create in your browser and it goes to the correction action) and it resolves a set of routevalues to a URL (which is what happens when you do URL generation like in an actionlink). The routes are tested in order, and the first one that declares a match wins, which means you don't always have the same result both directions if you haven't set them up right.
路由引擎做两件事;它将URL解析为一组routevalues(这是您在浏览器中键入/Network/Create时发生的情况,并执行修正操作),并将一组routevalues解析为一个URL(这是在actionlink中进行URL生成时发生的情况)。路线是按顺序测试的,第一个宣布比赛获胜的路线,这意味着如果你没有正确设置它们,你不会总是在两个方向上都有相同的结果。
Recommend doing route testing w/ the MVCContrib route helpers, which may help you narrow down your problem. Also, it's generally a bad practice (or at least unreliable) to rely on the "ambient" route values, as @Chad Moran suggested, if you choose oneof the more explicit overloads of ActionLink you may hve better results as well.
建议做路径测试w/ mvc悔过路径助手,这可以帮助你缩小你的问题。另外,像@Chad Moran所建议的那样,依赖“环境”路径值通常是不好的做法(或者至少是不可靠的),如果您选择一个更显式的超负载ActionLink,您可能也会得到更好的结果。
#2
0
Have you made sure everything is set up correctly with routing? This seems to indicate that no route was found where controller
and action
were sufficient routes.
你确定所有的东西都正确设置了吗?这似乎表明,如果控制器和操作是足够的路由,则没有找到任何路由。
#3
0
Since you're not supplying the Controller parameter of the method it's based on context. Is this call inside of another controller's context?
因为你没有提供方法的控制器参数它是基于上下文的。这个调用是在另一个控制器的上下文中吗?
Try this...
试试这个…
@Html.ActionLink("Create New", "Create", "Network")
@Html。ActionLink(“创建新”、“创造”、“网络”)