ASP.NET 4 RTM中的可选路由参数不再像以前那样工作

时间:2022-01-30 08:48:56

I upgraded my project to ASP.NET 4 RTM with ASP.NET MVC 2.0 RTM today.

我今天用ASP.NET MVC 2.0 RTM将项目升级到ASP.NET 4 RTM。

I was previously using ASP.NET 3.5 with ASP.NET MVC 2.0 RTM.

我以前使用ASP.NET 3.5与ASP.NET MVC 2.0 RTM。

Some of my routes don't work suddenly and I don't know why. I'm not sure if something changed between 3.5 and 4.0 - or if this was a regression type issue in the 4.0 RTM. (I never previously tested my app with 4.0).

我的一些路线突然不起作用,我不知道为什么。我不确定是否在3.5和4.0之间发生了变化 - 或者这是否是4.0 RTM中的回归类型问题。 (我以前从未用4.0测试过我的应用程序)。

I like to use Url.RouteUrl("route-name", routeParams) to avoid ambiguity when generating URLs. Here's my route definition for a gallery page. I want imageID to be optional (you get a thumbnail page if you don't specify it).

我喜欢使用Url.RouteUrl(“route-name”,routeParams)来避免生成URL时出现歧义。这是我对画廊页面的路线定义。我希望imageID是可选的(如果你没有指定它,你会得到一个缩略图页面)。

// gallery id
routes.MapRoute(
    "gallery-route",
    "gallery/{galleryID}/{imageID}/{title}",
    new { controller = "Gallery", action = "Index", 
          galleryID = (string) null, 
          imageID = (string) null, 
          title = (string) null}
);

In .NET 3.5 / ASP.NET 2.0 RTM / IIS7

在.NET 3.5 / ASP.NET 2.0 RTM / IIS7中

Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> /gallery/cats

Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")             
=>  /gallery/cats/4

Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")  
=>  /gallery/cats/4/tiddles

In .NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7

在.NET 4.0 RTM / ASP.NET 2.0 RTM / IIS7中

Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> null

Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")             
=>  /gallery/cats/4

Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles")  
=>  /gallery/cats/4/tiddles

Previously I could supply only the galleryID and everything else would be ignored in the generated URL. But now it's looking like I need to specify all the parameters up until title - or it gives up in determining the URL.

以前我只能提供galleryID,其他所有内容都会在生成的URL中被忽略。但是现在我看起来需要在标题之前指定所有参数 - 或者在确定URL时放弃它。

Incoming URLs work fine for /gallery/cats and that is correctly mapped through this rule with imageID and title both being assigned null in my controller.

传入的URL适用于/ gallery / cats,并通过此规则正确映射,其中imageID和title都在我的控制器中被指定为null。

I also tested the INCOMING routes with http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx and they all work fine.

我还用http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx测试了INCOMING路线,它们都运行良好。

2 个解决方案

#1


5  

The correct way of specifying optional parameters in ASP.NET MVC 2.0 is using the UrlParameter.Optional field:

在ASP.NET MVC 2.0中指定可选参数的正确方法是使用UrlParameter.Optional字段:

routes.MapRoute(
    "gallery-route",
    "gallery/{galleryID}/{imageID}/{title}",
    new
    {
        controller = "Gallery",
        action = "Index",
        galleryID = UrlParameter.Optional,
        imageID = UrlParameter.Optional,
        title = UrlParameter.Optional
    }
);

Assuming the following controller and action:

假设以下控制器和操作:

public class GalleryController : Controller
{
    public ActionResult Index(string galleryID, string imageID, string title)
    {
        return View();
    }
}

All these will work as expected:

所有这些都将按预期工作:

<%= Url.RouteUrl("gallery-route", new { galleryID = "cats" }) %><br/>
<%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4"}) %><br/>
<%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles" })%>

Render as:

/gallery/cats
/gallery/cats/4
/gallery/cats/4/tiddles

Remark: Tested on Windows 7 x64, Visual Studio 2010 RTM, ASP.NET MVC 2.0 project.

备注:在Windows 7 x64,Visual Studio 2010 RTM,ASP.NET MVC 2.0项目上测试。

#2


1  

I know the question is for MVC2 but in MVC3, the answer given by Darin Dimitrov will fail.

我知道问题出在MVC2上,但在MVC3中,Darin Dimitrov给出的答案将失败。

Phil Haack explained the problem and gave the work around in his blog: http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

Phil Haack解释了这个问题并在他的博客中解决了这个问题:http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

#1


5  

The correct way of specifying optional parameters in ASP.NET MVC 2.0 is using the UrlParameter.Optional field:

在ASP.NET MVC 2.0中指定可选参数的正确方法是使用UrlParameter.Optional字段:

routes.MapRoute(
    "gallery-route",
    "gallery/{galleryID}/{imageID}/{title}",
    new
    {
        controller = "Gallery",
        action = "Index",
        galleryID = UrlParameter.Optional,
        imageID = UrlParameter.Optional,
        title = UrlParameter.Optional
    }
);

Assuming the following controller and action:

假设以下控制器和操作:

public class GalleryController : Controller
{
    public ActionResult Index(string galleryID, string imageID, string title)
    {
        return View();
    }
}

All these will work as expected:

所有这些都将按预期工作:

<%= Url.RouteUrl("gallery-route", new { galleryID = "cats" }) %><br/>
<%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4"}) %><br/>
<%= Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4", title = "tiddles" })%>

Render as:

/gallery/cats
/gallery/cats/4
/gallery/cats/4/tiddles

Remark: Tested on Windows 7 x64, Visual Studio 2010 RTM, ASP.NET MVC 2.0 project.

备注:在Windows 7 x64,Visual Studio 2010 RTM,ASP.NET MVC 2.0项目上测试。

#2


1  

I know the question is for MVC2 but in MVC3, the answer given by Darin Dimitrov will fail.

我知道问题出在MVC2上,但在MVC3中,Darin Dimitrov给出的答案将失败。

Phil Haack explained the problem and gave the work around in his blog: http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

Phil Haack解释了这个问题并在他的博客中解决了这个问题:http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx