ASP.NET MVC:出现空ActionLinks

问题描述:

我使用的是默认路由,这样我就不需要指定控制器。

I'm using a default route, so that I don't need to specify the controller.

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

有了这个,我可以创造像myapp.com/Customers而不是myapp.com/Home/Customers网址

With this, I can create URLs like myapp.com/Customers rather than myapp.com/Home/Customers

当我在本地测试,一切都很好。当我上传的现场版,与Html.ActionLink产生的任何联系都是空的。我知道我使用的Html.ActionLink正确的,因为它工作本地罚款:

When I test locally, everything is fine. When I upload a live version, any links generated with Html.ActionLink are empty. I know I'm using Html.ActionLink correctly, because it works fine locally:

//                   Title                 Action      Controller
<%: Html.ActionLink("Manage My Settings", "Settings", "Home") %>

我已经删除所有路线,但默认情况下,试图用不带控制器等等。我甚至试图恢复到其在路由控制器指定ActionLink的,例如:

I've removed all routes but the default, tried specifying ActionLink with and without the controller etc. I even tried reverting to having the controller in the route, e.g:

"{controller}/{action}/{id}"

没有什么工作现场。一切工作本地。去稍微疯狂。

Nothing works live. Everything works locally. Going slightly mad.

更新:

OK,做了一个奇怪的发现。其实我有身份证后,被称为第一个可选UrlParameter。我愣神没有包括在本例中,因为我认为这并没有区别。如果我把它拿出来,事情似乎工作。

OK, made a strange discovery. I actually had another optional UrlParameter after id, called page. I stupidly didn't include it in the example because I thought it made no difference. If I take it out, things seem to work.

所以,实际上,这个作品:

So, actually, this works:

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

这个的作品!

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

不过的这个的做的的作品

routes.MapRoute(
   "Default", 
   "{action}/{id}/{page}", 
    new { controller = "Home", action = "Index", 
    id = UrlParameter.Optional, page = UrlParameter.Optional }
);

为什么不呢?

找到了答案!有使用两个连续的可选UrlParameters当MVC3一个bug,通过菲尔哈克越详细,这里路由回归与两连续,可选的URL参数

Found the answer! There's a bug in MVC3 when using two consecutive optional UrlParameters, as detailed by Phil Haack, here routing-regression-with-two-consecutive-optional-url-parameters

您需要首先声明一个版本的路线只有一个可选参数。因此,

You need to first declare a version of the route with only one optional parameter. So

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

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

现在似乎真的明显。如果我其实包括所有的细节我敢肯定Serghei或其他人会看到这个问题,所以感谢所有帮助家伙!

Seems really obvious now. If I'd actually included all the details I'm sure Serghei or someone else would have seen the problem, so thanks for all the help guys!