我有一个财务总监:
public class ProfileController : Controller
{
public ActionResult Index( long? userkey )
{
...
}
public ActionResult Index( string username )
{
...
}
}如何为此操作定义MapRoute,工作方式如下:
mysite.com/Profile/8293378324043043840
这必须是第一个行动
mysite.com/Profile/MyUserName
这必须转到第二步
我有这条第一步的路线
routes.MapRoute( name: "Profile" , url: "Profile/{userkey}" , defaults: new { controller = "Profile" , action = "Index" } );我需要添加另一个MapRoute吗?或者可以为这两个操作更改当前的MapRoute吗?
发布于 2012-12-27 13:27:10
首先,如果您使用相同的Http谓词(在您的示例中是GET),则不能重载控制器操作,因为您需要有唯一的操作名称。
因此,您需要将您的行为命名为不同的:
public class ProfileController : Controller
{
public ActionResult IndexKey( long? userkey )
{
...
}
public ActionResult IndexName( string username )
{
...
}
}或者您可以使用ActionNameAttribute为您的操作指定不同的名称:
public class ProfileController : Controller
{
[ActionName("IndexKey")]
public ActionResult Index( long? userkey )
{
...
}
[ActionName("IndexName")]
public ActionResult Index( string username )
{
...
}
}然后,您将需要使用using route constraints在userkey上的两条路由作为一个数值来设置您的操作:
routes.MapRoute(name: "Profile", url: "Profile/{userkey}",
defaults: new { controller = "Profile", action = "IndexKey" },
constraints: new { userkey = @"\d*"});
routes.MapRoute(name: "ProfileName", url: "Profile/{userName}",
defaults: new {controller = "Profile", action = "IndexName"});https://stackoverflow.com/questions/14055294
复制相似问题