简体   繁体   English

ASP.NET MVC - 提取 URL 的参数

[英]ASP.NET MVC - Extract parameter of an URL

I'm trying to extract the parameters of my URL, something like this.我正在尝试提取我的 URL 的参数,就像这样。

/Administration/Customer/Edit/1 /管理/客户/编辑/1

extract: 1提取物: 1

/Administration/Product/Edit/18?allowed=true /Administration/Product/Edit/18?allowed=true

extract: 18?allowed=true摘录: 18?allowed=true

/Administration/Product/Create?allowed=true /Administration/Product/Create?allowed=true

extract: ?allowed=true提取: ?allowed=true

Someone can help?有人可以帮忙吗? Thanks!谢谢!

Update更新

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

Will match all your examples将匹配您的所有示例


It is not entirely clear what you are trying to achieve.您要实现的目标并不完全清楚。 MVC passes URL parameters for you through model binding. 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();
  }

}

Adding a route mapping to your global.asax.cs file before the default will handle the /administration/ part.在默认处理 /administration/ 部分之前,将路由映射添加到 global.asax.cs 文件。 Or you might want to look into MVC Areas.或者您可能想查看 MVC 区域。

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

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如果它是您所追求的原始 URL 数据,那么您可以使用控制器操作中可用的各种 URL 和请求属性之一

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

It sounds like Request.Url.PathAndQuery could be what you want.听起来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"];
public ActionResult Index(int id,string value)

This function get values form URL After that you can use below function此函数从 URL 获取值之后,您可以使用以下函数

Request.RawUrl - Return complete URL of Current page Request.RawUrl - 返回当前页面的完整 URL

RouteData.Values - Return Collection of Values of URL RouteData.Values - 返回 URL 值的集合

Request.Params - Return Name Value Collections Request.Params - 返回名称值集合

I wrote this method:我写了这个方法:

    private string GetUrlParameter(HttpRequestBase request, string parName)
    {
        string result = string.Empty;

        var urlParameters = HttpUtility.ParseQueryString(request.Url.Query);
        if (urlParameters.AllKeys.Contains(parName))
        {
            result = urlParameters.Get(parName);
        }

        return result;
    }

And I call it like this:我这样称呼它:

string fooBar = GetUrlParameter(Request, "FooBar");
if (!string.IsNullOrEmpty(fooBar))
{

}

You can get these parameter list in ControllerContext.RoutValues object as key-value pair.您可以在 ControllerContext.RoutValues 对象中以键值对的形式获取这些参数列表。

You can store it in some variable and you make use of that variable in your logic.您可以将其存储在某个变量中,然后在逻辑中使用该变量。

In order to get the values of your parameters, you can use RouteData .为了获取参数的值,您可以使用RouteData

More context would be nice.更多的上下文会很好。 Why do you need to "extract" them in the first place?为什么首先需要“提取”它们? You should have an Action like:你应该有一个像这样的动作:

public ActionResult Edit(int id, bool allowed) {}

I'm not familiar with ASP.NET but I guess you could use a split function to split it in an array using the / as delimiter, then grab the last element in the array (usually the array length -1) to get the extract you want.我对 ASP.NET 不熟悉,但我想您可以使用split函数将其拆分为使用 / 作为分隔符的数组,然后获取数组中的最后一个元素(通常是数组长度 -1)以获取提取物你要。

Ok this does not seem to work for all the examples.好的,这似乎不适用于所有示例。

What about a regex?正则表达式呢?

.*(/|[a-zA-Z]+\?)(.*)

then get that last subexpression (.*) , I believe it's $+ in .Net, I'm not sure然后得到最后一个子表达式(.*) ,我相信它是 .Net 中的$+ ,我不确定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM