ASP.NET MVC Routing Assistance
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=v开发者_StackOverflowal1&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.
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.
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();
}
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){
....
}
精彩评论