ASP.NET MVC - 一个URL的参数提取 [英] ASP.NET MVC - Extract parameter of an URL

查看:242
本文介绍了ASP.NET MVC - 一个URL的参数提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取我的URL的参数,这样的事情。

I'm trying to extract the parameters of my URL, something like this.

/行政/客户/编辑/ 1

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

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

有人能帮忙吗?谢谢!

推荐答案

更新

RouteData.Values["id"] + Request.Url.Query

将匹配所有的例子

Will match all your examples

这是不完全清楚你想达到什么目的。 MVC模式,通过传递URL参数你绑定。

It is not entirely clear what you are trying to achieve. MVC passes URL parameters for you through model binding.

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文件的默认之前将处理/管理/一部分。或者你可能想看看MVC领域。

Adding a route mapping to your global.asax.cs file before the default will handle the /administration/ part. Or you might want to look into MVC Areas.

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

如果这是你所追求的原始URL数据,那么你可以用在你的控制器动作提供的各种URL和请求属性之一

If it's the raw URL data you are after then you can use one of the various URL and Request properties available in your controller action

string url = Request.RawUrl;
string query= Request.Url.Query;
string isAllowed= Request.QueryString["allowed"];

这听起来像 Request.Url.PathAndQuery 可能是你想要的东西。

如果你想获得的原始发布的数据,你可以使用

If you want access to the raw posted data you can use

string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];

这篇关于ASP.NET MVC - 一个URL的参数提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆