ASP.NET MVC - 提取URL的参数

Kim*_*jan 55 c# asp.net-mvc asp.net-mvc-routing

我正在尝试提取我的URL的参数,就像这样.

/行政/客户/编辑/ 1

提取物: 1

/管理/产品/编辑/ 18?允许=真

提取物: 18?allowed = true

/管理/产品/创建?允许=真

extract:?allowed = true

有人可以帮忙吗?谢谢!

Dav*_*enn 89

更新

RouteData.Values["id"] + Request.Url.Query
Run Code Online (Sandbox Code Playgroud)

将匹配您的所有示例


你想要实现的目标并不完全清楚.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();
  }

}
Run Code Online (Sandbox Code Playgroud)

在默认情况下将路由映射添加到global.asax.cs文件将处理/ administration/part.或者你可能想看看MVC领域.

routes.MapRoute(
  "Admin", // Route name
  "Administration/{controller}/{action}/{id}", // URL with parameters
  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
Run Code Online (Sandbox Code Playgroud)

如果它是您之后的原始URL数据,则可以使用控制器操作中可用的各种URL和请求属性之一

string url = Request.RawUrl;
string query= Request.Url.Query;
string isAllowed= Request.QueryString["allowed"];
Run Code Online (Sandbox Code Playgroud)

听起来Request.Url.PathAndQuery可能是你想要的.

如果要访问可以使用的原始发布数据

string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];
Run Code Online (Sandbox Code Playgroud)


小智 7

public ActionResult Index(int id,string value)
Run Code Online (Sandbox Code Playgroud)

此函数从URL获取值。之后,您可以使用以下函数

Request.RawUrl -返回当前页面的完整网址

RouteData.Values -返回URL值的集合

Request.Params -返回名称值集合