ASP.NET MVC - Extract URL parameter

I am trying to extract the parameters of my url, something like this.

/ Administration / Client / Edit / 1

extract: 1

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

extract: 18? allowed = true

/ Administration / Product / Create? Allowed = true

extract :? allowed = true

Can anybody help? Thank!

+42
c # asp.net-mvc asp.net-mvc-routing
Feb 15 2018-11-15T00:
source share
5 answers

Update

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

Will match all your examples




It is not clear what you are trying to achieve. MVC passes you URLs 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(); } } 

Adding route mapping to the global.asax.cs file before the / administration / part element is processed by default. Or you can look in the field of MVC.

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

If this is raw URL data after that, you can use one of the various URL and query properties available in your controller action.

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

It looks like Request.Url.PathAndQuery might be what you want.

If you need access to the source data, you can use

 string isAllowed = Request.Params["allowed"]; string id = RouteData.Values["id"]; 
+71
Feb 15 2018-11-15T00:
source share
 public ActionResult Index(int id,string value) 

This function gets the values โ€‹โ€‹of the URL form. After that you can use the function below

Request.RawUrl - return the full URL of the current page

RouteData.Values - Return a collection of URL values

Request.Params - Return Name Collection Value

+3
03 Sep '15 at 5:55
source share

You can get this list of parameters in the ControllerContext.RoutValues โ€‹โ€‹object as a key-value pair.

You can save it in some variable, and you use this variable in your logic.

+1
Feb 15 '11 at 13:17
source share

To get the values โ€‹โ€‹of your parameters, you can use 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) {}

0
Feb 15 2018-11-15T00:
source share

I am not familiar with ASP.NET, but I think you could use the split function to split it in the array using the / as separator, and then grab the last element into the array (usually the length of the array is -1) to get the desired extract.

Good thing this does not work for all examples.

What about regex?

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

then you get this last subexpression (.*) , I believe this is $+ in .Net, I'm not sure

-2
Feb 15 '11 at 13:14
source share



All Articles