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

发布于 2024-08-28 19:23:32 字数 1912 浏览 4 评论 0原文

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

我之前使用的是 ASP.NET 3.5ASP.NET MVC 2.0 RTM

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

我喜欢使用 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}
);

在 .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

中 在 .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

以前,我只能提供 galleryID ,而生成的 URL 中的其他所有内容都将被忽略。但现在看来我需要指定 title 之前的所有参数 - 否则它会放弃确定 URL。

传入 URL 对于 /gallery/cats 工作正常,并且通过此规则正确映射,imageIDtitle 均被分配 null< /code> 在我的控制器中。

我还使用 http://haacked.com/ 测试了传入路由archive/2008/03/13/url-routing-debugger.aspx 它们都工作正常。

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

I was previously using ASP.NET 3.5 with 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).

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).

// 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

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

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.

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-09-04 19:23:32

在 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
    }
);

假设以下控制器和操作:

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

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

<%= 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" })%>

渲染为:

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

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

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

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.

可爱咩 2024-09-04 19:23:32

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文