任何人都可以在 .NET 4.0/MVC3 中重新创建我在具有 2 个或更多可选参数的路由中遇到的以下错误吗? [英] Can anyone recreate the following bug I'm experiencing in routes with 2 or more optional parameters, in .NET 4.0 / MVC3?

查看:14
本文介绍了任何人都可以在 .NET 4.0/MVC3 中重新创建我在具有 2 个或更多可选参数的路由中遇到的以下错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果您认为自己在 .NET 框架中发现了错误,那么您很可能是错的,但这就是我写这个问题的原因,所以请听我说.

I know that if you think you found a bug in the .NET framework you are most likely wrong, but that's why I'm writing this question, so please hear me out.

我相当肯定 .NET 3.5 和 .NET 4.0 中的路由在可选参数方面存在差异.特别是如果您的路线中有多个可选参数.我无法在 .NET 4.0 或 MVC 3 的任何发行说明中找到这一重大更改,因此我将其称为错误.

I am fairly certain that there is a difference between the routes in .NET 3.5 and .NET 4.0 when it comes to optional parameters. Specifically if you have more than one optional parameter in your route. I have not been able to find this breaking change noted in any release notes of either .NET 4.0 or MVC 3 so thats why I call it a bug.

此错误仅在您尝试使用 mvc 中的 url 或 html helpers 之类的代码构建路由 url 时才会出现.如果您实际上在浏览器中请求 url,在实际的 mvc 应用程序中,它工作得很好.因此,如果我下面的测试应用程序是一个实际的 mvc 应用程序,那么如果您尝试请求/root/test1",就不会有问题.

This bug only manifests itself when you try to build a route url using code like the url or html helpers in mvc. If you actually request the url in a browser, in an actual mvc app, it works just fine. So if my test app below were an actual mvc app there wouldn't be a problem if you tried to request '/root/test1'.

我需要你做的是运行下面的测试程序,它应该是不言自明的,但基本上它只是设置了一个带有一些可选参数的路由.

What I need you to do is run the following test program, it should be fairly self-explanatory, but basically it just sets up a route with some optional paramters.

  1. 创建一个新的 .NET 4 控制台应用程序

  1. Create a new .NET 4 console application

将目标框架更改为.NET Framework 4"而不是.NET Framework 4 Client Profile"

Change the target framework to '.NET Framework 4' instead of '.NET Framework 4 Client Profile'

添加对以下内容的引用:
System.Web 4.0
System.Web.Routing 4.0
System.Web.Mvc 3.0

Add references to:
System.Web 4.0
System.Web.Routing 4.0
System.Web.Mvc 3.0

将以下代码粘贴到 program.cs 文件中,覆盖之前的所有内容:

Paste following code to the program.cs file, overwriting any previous content:

using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

public class Program
{
    static void Main()
    {
        var httpCtx = new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://localhost/", null), new HttpResponse(new StringWriter())));

        var routes = RouteTable.Routes;
        routes.MapRoute("Test", "root/{test1}/{test2}/{test3}", new { test2 = UrlParameter.Optional, test3 = UrlParameter.Optional });

        var context = new RequestContext(httpCtx , new RouteData());

        var url = new UrlHelper(context);

        var expected1 = "/root/test1";
        var expected2 = "/root/test1/test2";
        var expected3 = "/root/test1/test2/test3";

        var actual1 = url.RouteUrl("Test", new { test1 = "test1" });
        var actual2 = url.RouteUrl("Test", new { test1 = "test1", test2 = "test2" });
        var actual3 = url.RouteUrl("Test", new { test1 = "test1", test2 = "test2", test3 = "test3" });

        var result1 = actual1 == expected1;
        var result2 = actual2 == expected2;
        var result3 = actual3 == expected3;

        Console.WriteLine("Test 1: {0} ({1})", result1 ? "Success" : "Fail", result1 ? string.Format("'{0}'", actual1) : string.Format("Expected '{0}' but was '{1}'", expected1, actual1 ?? "<null>"));
        Console.WriteLine("Test 2: {0} ({1})", result2 ? "Success" : "Fail", result2 ? string.Format("'{0}'", actual2) : string.Format("Expected '{0}' but was '{1}'", expected2, actual2 ?? "<null>"));
        Console.WriteLine("Test 3: {0} ({1})", result3 ? "Success" : "Fail", result3 ? string.Format("'{0}'", actual3) : string.Format("Expected '{0}' but was '{1}'", expected3, actual3 ?? "<null>" ));
        Console.ReadLine();
    }
}

  • 运行程序,并注意生成的 url.
    在我的机器上,结果是:

  • Run program, and note the resulting urls.
    On my machine the result is:

    Test 1: Fail    (expected '/root/test1' but was '<null>')
    Test 2: Success ('/root/test1/test2')
    Test 3: Success ('/root/test1/test2/test3')
    

  • 现在将目标框架更改为 .NET 3.5 添加对以下内容的引用:
    System.Web.Mvc 2.0
    System.Web.Routing 3.5
    System.Web.Abstrations 3.5

  • Now change the target framework to .NET 3.5 add references to:
    System.Web.Mvc 2.0
    System.Web.Routing 3.5
    System.Web.Abstrations 3.5

    再次运行程序,看到3个测试用例都成功了.
    这次的结果是:

    Run the program again and see the 3 test cases all succeed.
    This time the result is:

    Test 1: Success ('/root/test1')  
    Test 2: Success ('/root/test1/test2')  
    Test 3: Success ('/root/test1/test2/test3')
    

  • 所以在我看来 .NET 4 或 MVC 3 中存在一个错误.如果您发现相同的问题,请在连接中对以下问题进行投票:https://connect.microsoft.com/VisualStudio/反馈/细节/630568/url-routing-with-two-optional-parameters-unspecified-fails-on-asp-net-mvc3-rc2#details

    So it seems to me there is a bug in the .NET 4 or MVC 3. If you find the same issue please vote on the following issue in connect: https://connect.microsoft.com/VisualStudio/feedback/details/630568/url-routing-with-two-optional-parameters-unspecified-fails-on-asp-net-mvc3-rc2#details

    如果您认为测试程序中存在问题,请随时在常规 MVC 应用程序中对此进行测试.

    Feel free to test this out in a regular MVC application if you think theres something wrong in the test program.

    推荐答案

    所以,Phil Haack 刚刚发布了一个 博文 详细说明这是一个已知问题,将在 .NET 框架的下一个版本中修复,或者如果我们幸运的是修复了特定 dll 的错误.我不确定他们将如何修复它.他们是否会取缔超过 1 个可选参数,或者他们是否会按照我在 Darin 回答的评论中概述的方式解决它.至少他们知道这一点,并且可以采取措施在未来使用多个可选参数来指定预期行为.

    So, Phil Haack just released a blog post detailing that this is a known issue and will be fixed in the next release of the .NET framework, or maybe if we are lucky as a bug fix to the particular dll. I'm not exactly sure how they will fix it though. Whether they will outlaw more than 1 optional parameter, or whether they will resolve it in the manner I've outlined in the comments to Darin's answer. At least they know about it and can take steps to specify the expected behavior with more than one optional parameter more in the future.

    这篇关于任何人都可以在 .NET 4.0/MVC3 中重新创建我在具有 2 个或更多可选参数的路由中遇到的以下错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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