2011-05-25 81 views
2

我有一个ASP.NET MVC 2的Web应用程序,它应该从一个相当愚蠢的系统接收请求。有问题的系统希望它是一个PHP站点。不是。我获得请求的形式为:ASP.NET MVC Routing Assistance

 
http://myIP/index.php?oa=val1&da=val1&ud=val1 

我有一个控制器的方法

Index(string oa, string da, string ud) 

但我不知道如何让这个请求路由到该控制器。我已经试过

routes.MapRoute( 
    "R", 
    "index.php/{oa}/{da}/{ud}", 
    new { controller = "Home", action = "Index" } 
); 

但无济于事。它适用于索引格式为Index.php/val1/val2/val3的请求,但当请求如上所示时,它会生成一个404.

谢谢。

回答

1

我会的路线简单地映射到 “PHP” 页面。查询字符串参数不会转换为路由数据。

routes.MapRoute("R","index.php", new { controller = "Home", action = "Index" }); 

,然后在控制器上你的行动

public ActionResult Index() { 
    string oa = Request.QueryString["oa"]; 
    string da = Request.QueryString["da"]; 
    string ud = Request.QueryString["ud"]; 

    //do the rest of your logic here (obviously) 

    return View(); 
} 
2

该路由不起作用,因为QueryString不是RouteData的一部分。最好将路由值分开以查询参数。

我只是简单地映射index.php,然后访问控制器中的查询字符串。

+0

肖偷了你的答案! – Terry 2011-06-04 17:21:00

0

您可以使用此路由:

routes.MapRoute(
      "php", 
      "index.php", 
      new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional }); 

,并使用此方法:

public ActionResult Index(string oa, string da, string ud){ 
    .... 
}