更新
RouteData.Values["id"] + Request.Url.Query
将匹配您的所有示例
您要达到的目标并不完全清楚。 MVC 通过模型绑定为你传递 URL 参数。
public class CustomerController : Controller {
public ActionResult Edit(int id) {
int customerId = id //the id in the URL
return View();
}
}
public class ProductController : Controller {
public ActionResult Edit(int id, bool allowed) {
int productId = id; // the id in the URL
bool isAllowed = allowed // the ?allowed=true in the URL
return View();
}
}
在默认情况下将路由映射添加到 global.asax.cs 文件将处理 /administration/ 部分。或者您可能想研究 MVC 领域。
routes.MapRoute(
"Admin", // Route name
"Administration/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
如果它是您所追求的原始 URL 数据,那么您可以使用控制器操作中可用的各种 URL 和请求属性之一
string url = Request.RawUrl;
string query= Request.Url.Query;
string isAllowed= Request.QueryString["allowed"];
听起来Request.Url.PathAndQuery 可能是您想要的。
如果你想访问原始发布的数据,你可以使用
string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];