【问题标题】:MVC 3 does not allow '.' in URL path when using IIS6MVC 3 不允许 '.'在使用 IIS6 时的 URL 路径中
【发布时间】:2011-03-21 15:43:25
【问题描述】:

运行 IIS6
所以, '。'在 IIS6 中不起作用,但它们在 Visual Studio 调试器和 IIS7 中工作正常。这是重现的步骤。

复制步骤:
- 从一个空白的 MVC 3 项目开始。
- 添加一个名为“索引”的新视图并接受默认值。
- 配置 RegisterRoutes() 如下:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "QuerySomething",
        "QueryStuff/Index/{*aString}",
        new { controller = "QueryStuff", action = "Index", aString = UrlParameter.Optional } // Parameter defaults
    );

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

}

现在,添加一个返回 Json 的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

    namespace MvcApplication1.Controllers
    {
        public class QueryStuffController : Controller
        {
            //
            // GET: /QueryStuff/

            public ActionResult Index(string aString)
            {
                return Json("aString:oye",JsonRequestBehavior.AllowGet);
            }

        }
    }
  1. 验证页面是否可访问:

    http://serverName/QueryStuff/Index/someInfo

你应该得到 http 200。

  1. 现在尝试使用 '.' 到达那里。在路径中

    http://serverName/QueryStuff/Index/someInfo.com

您应该会收到 http 404 错误。 (请注意,通过 Visual Studio 调试器运行时,此错误无法重现。必须将代码部署到 IIS。)

更新
我将 email 地址的正则表达式修改为 route,这让问题变得更糟。

    routes.MapRoute(
        "QuerySomething",
        "QueryStuff/Index/{aString}"
        , new { controller = "QuerySomething", action = "Index" },
        new { aString = @"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b" }
        ); 

每次都是 404。

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    我不认为这是一个 MVC 错误,更多的是 http 的限制?我们遇到了同样的问题,所以最终交换了“。”为了 ”!”在 URL 中,然后将它们转换回“。”在控制器中。

    【讨论】:

    • 这是我希望避免做的事情。
    • @ColinDesmond:我不确定您将使用“http 的限制”位去哪里。 HTTP 对. 没有任何问题,它们通常用于 URL 和 URI(并且 Internet 可路由域名需要它们)。也许您的意思是 RegEx 或 Web 服务器在路由规则中对 . 的解析?
    • @Colin Desmond - 我确信这个问题与 IIS6 有关。由于 BuildStarted 无法在 IIS7 上重现该问题,再加上这件事在 Visual Studio 调试器中运行良好。我正在使用您的想法来替换“。”带有'!'的字符。在@Haacked 等人确认问题之前,它作为一种解决方法非常有效。
    • 经过一番探讨,这可能会有所帮助stackoverflow.com/questions/294495/…
    • @Colin Desmond - 纠正了它。仍然令人惊讶的是,这只发生在 IIS 6 中。
    【解决方案2】:
    routes.MapRoute(
        "QuerySomething",
        "QueryStuff/Index/{*aString}",
        new { controller = "QueryStuff", action = "Index", aString = UrlParameter.Optional } // Parameter defaults
    );
    

    您忘记了路径中的通配符。 (注意上面的aString)但是,使用它们时要注意的一件事是它也将匹配http://serverName/QueryStuff/Index/something.com/blah/blah/blah/blah 点是文件扩展名分隔符,这就是它不包括在内的原因。如果你知道你总是会有扩展,你也可以这样做:

    routes.MapRoute(
        "QuerySomething",
        "QueryStuff/Index/{aString}.{extension}",
        new { controller = "QueryStuff", action = "Index", aString = UrlParameter.Optional } // Parameter defaults
    );
    

    【讨论】:

    • @BuildStarted - 我仍然收到带有 {aString} 的 404。还尝试了 {*aString}。没有差异。在较小的说明中,这并不能解释为什么使用 Visual Studio 调试器时不会发生错误。
    • 刚刚在这里测试过,效果很好。 {*aString*} 不起作用,它必须是 {*aString}。我正在运行 IIS7,所以这可能是一个问题?你在运行 IIS6 吗?
    • 关于调试器。这可能取决于您要发布到的 IIS 版本。
    • @BuildStarted - 也许是这样:IIS6
    • @BuildStarted - 这将有助于解释为什么它在我的 Visual Studio 调试器中运行良好,但在 IIS 中崩溃......如果它的 IIS 版本无法正确处理路由。跨度>
    猜你喜欢
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    相关资源
    最近更新 更多