ASP.NET MVC与参数基本路由 [英] ASP.NET MVC basic routing with parameters

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

问题描述

我一直在努力学习ASP.NET MVC 3,事情从路由方面,无论我尝试我刚才似乎无法让他们完全正确分开进展顺利。

I have been trying to learn ASP.NET MVC 3 and things are going well apart from the routing aspect, whatever I try I just can't seem to get them quite right.

我的主网页上的ActionLink的:

I have an ActionLink on the main page:

@Html.ActionLink("Contracts", "List", "Contract", 
                 new { User.Identity.Name, page=1 })

这意味着在ContractController访问此方法:

Which is meant to access this method in the ContractController:

public ViewResult List(string user, int page = 1)
{
    //snip
}

我的路线是:

 routes.MapRoute(
     null, 
     "Page{page}",
     new { Controller = "Contract", action = "List" }
 );

 routes.MapRoute(
     null,
     "Page{page}",
     new { Controller = "Contract", action = "List", user = "", page = 1 }
 );

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

该链接现在将返回404错误,因为它无法找到控制器'家',这显然意味着它并没有使用第一个途径要么行动清单。

The link now will return a 404 error as it can't find the action 'List' in the controller 'Home', which obviously means it didn't use either of the first routes.

一切工作之前,我试图参数添加到ActionLink的,所以基本上,我究竟做错了什么?

Everything worked before I tried to add parameters to the ActionLink, so basically, what am I doing wrong?

非常感谢。

推荐答案

亚历克斯,

您是绝对正确的做所有的其他位,但是ActionLink的缺少一个参数,试试这个你ActionLink的:

You're doing all the other bits absolutely correctly, however the actionlink has a missing parameter, try this for your actionlink:

@Html.ActionLink("Contracts", "List", "Contract", 
             new { User.Identity.Name, page = 1 }, null)

添加作为最后的参数( htmlAttributes )是所有在此方案中的对你的思念(有9重载 Html.ActionLink ,所以很容易错过正确执行)。

Adding the null as the final param (htmlAttributes) is all that's missing for you in this scenario (there are 9 overloads for Html.ActionLink, so it's VERY easy to miss the correct implementation).

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

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