次のような URL のパラメータを抽出しようとしています。
/管理/顧客/編集/1
抽出する:1
/管理/製品/編集/18?allowed=true
抽出する:18?許可=true
/管理/製品/作成?allowed=true
抽出する:?許可=true
誰か助けてくれませんか? ありがとう!
ベストアンサー1
アップデート
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"];