ASP.NET MVC 路由协助

发布于 2024-11-09 18:19:16 字数 505 浏览 7 评论 0原文

我有一个 ASP.NET MVC 2 Web 应用程序,它应该从一个相当愚蠢的系统接收请求。相关系统期望它是一个 PHP 站点。它不是。我收到的请求的形式如下:

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。

谢谢。

I have an ASP.NET MVC 2 web application which is supposed to receive a request from a rather stupid system. The system in question expects it to be a PHP site. It's not. The request I get is of the form:

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

I have a controller with a method

Index(string oa, string da, string ud)

But I don't know how to get this request routed to this controller. I have tried

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

But to no avail. It works if the request comes in the format Index.php/val1/val2/val3, but when the request comes as shown above, it generates a 404.

Thanks.

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

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

发布评论

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

评论(3

夕嗳→ 2024-11-16 18:19:17

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

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

The route doesn't work because the QueryString is not part of the RouteData. It's best to keep route values separate to query params.

I would simply map index.php and then access the query string in your controller.

梦里寻她 2024-11-16 18:19:17

我只是将路由映射到“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();
}

I would simply map the route to the "php" page. The query string parameters will not transpose into the route data.

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

and then for your action on the controller

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();
}
披肩女神 2024-11-16 18:19:17

您可以使用此路由:

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

并使用此方法:

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

You can use the this routing:

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

and use this method:

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