2011-03-21 43 views
2

運行IIS6
所以,'。'不在IIS6中工作,但它們在Visual Studio調試器和IIS7中正常工作。以下是重現的步驟。MVC 3不允許'。'在URL路徑中使用IIS6時

重現步驟:
- 從一個空白的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調試器中運行時,不reproduceable。我們必須將代碼部署到IIS)

UPDATE
我修改正則表達式來routeemail地址,它使問題更糟。

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

有了這個它每次404。

回答

4

我不認爲這是一個MVC錯誤,因此,更多的限制http?我們遇到了同樣的問題,因此最終交換了「。」爲「!」在URL中,然後將它們轉換回「。」在控制器中。

+1

這是我希望避免做的事情。 – 2011-03-21 17:14:54

+1

@ColinDesmond:我不確定你要去哪裏「限制http」位。 HTTP在'.'中沒有問題,它們通常用於URL和URI(以及Internet可路由的域名需要它們)。也許你的意思是RegEx或Web服務器在路由規則中解析'.'? – Powerlord 2011-03-21 18:30:14

+1

@Colin Desmond - 我確信這個問題與IIS6有關。由於BuildStarted無法重現IIS7上的問題,而且事實上這件事在Visual Studio調試器中可以正常工作。我用你的想法來取代'。'字符與'!'。它可以很好地解決問題,直到像@Haacked這樣的人確認問題。 – 2011-03-21 19:08:27

1
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 
); 
+0

@BuildStarted - 我仍然用{* aString}得到404。還嘗試了{* aString *}。沒有差異。另一方面,這並不能解釋爲什麼使用Visual Studio調試器時不會出現錯誤。 – 2011-03-21 17:13:59

+0

剛剛在這裏測試過,它工作正常。 '{* aString *}'不起作用,它必須是'{* aString}'。我正在運行IIS7,所以這可能是一個問題呢?你在運行IIS6嗎? – Buildstarted 2011-03-21 18:10:32

+0

關於調試器。它可能取決於您要發佈到哪個版本的IIS。 – Buildstarted 2011-03-21 18:11:41