I'm trying to extract the parameters of my URL, something like this.
我试着提取URL的参数,像这样。
/Administration/Customer/Edit/1
/管理/客户/编辑/ 1
extract: 1
/Administration/Product/Edit/18?allowed=true
/管理/产品/编辑/ 18 ?允许= true
extract: 18?allowed=true
/Administration/Product/Create?allowed=true
/管理/产品/创建?允许= true
extract: ?allowed=true
Someone can help? Thanks!
有人可以帮忙吗?谢谢!
6 个解决方案
#1
79
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. Or you might want to look into MVC Areas.
添加到global.asax的路由映射。cs文件在默认情况下将处理/管理/部件。或者您可能需要查看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"];
#2
3
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
请求。返回当前页面的完整URL
RouteData.Values
- Return Collection of Values of URL
RouteData。值——返回URL的值的集合
Request.Params
- Return Name Value Collections
请求。返回名称值集合
#3
1
You can get these parameter list in ControllerContext.RoutValues object as key-value pair.
您可以在ControllerContext中获得这些参数列表。routvalue对象作为键-值对。
You can store it in some variable and you make use of that variable in your logic.
你可以把它存储在某个变量中你可以在你的逻辑中使用那个变量。
#4
0
In order to get 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) {}
更多的背景会更好。为什么你首先需要“提取”它们?您应该有如下操作:public ActionResult Edit(int id, bool allowed) {}
#5
0
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))
{
}
#6
-3
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。但是我猜您可以使用一个split函数使用/ as分隔符将它分割到一个数组中,然后获取数组中的最后一个元素(通常是数组长度-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中是$+
#1
79
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. Or you might want to look into MVC Areas.
添加到global.asax的路由映射。cs文件在默认情况下将处理/管理/部件。或者您可能需要查看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"];
#2
3
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
请求。返回当前页面的完整URL
RouteData.Values
- Return Collection of Values of URL
RouteData。值——返回URL的值的集合
Request.Params
- Return Name Value Collections
请求。返回名称值集合
#3
1
You can get these parameter list in ControllerContext.RoutValues object as key-value pair.
您可以在ControllerContext中获得这些参数列表。routvalue对象作为键-值对。
You can store it in some variable and you make use of that variable in your logic.
你可以把它存储在某个变量中你可以在你的逻辑中使用那个变量。
#4
0
In order to get 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) {}
更多的背景会更好。为什么你首先需要“提取”它们?您应该有如下操作:public ActionResult Edit(int id, bool allowed) {}
#5
0
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))
{
}
#6
-3
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。但是我猜您可以使用一个split函数使用/ as分隔符将它分割到一个数组中,然后获取数组中的最后一个元素(通常是数组长度-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中是$+