66

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

/Administration/Customer/Edit/1

extract: 1

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

extract: 18?allowed=true

/Administration/Product/Create?allowed=true

extract: ?allowed=true

Someone can help? Thanks!

Kim Tranjan
  • 4,521
  • 3
  • 39
  • 38

6 Answers6

107

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.

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.

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

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.

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

string isAllowed = Request.Params["allowed"];
string id = RouteData.Values["id"];
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
David Glenn
  • 24,412
  • 19
  • 74
  • 94
9
public ActionResult Index(int id,string value)

This function get values form URL After that you can use below function

Request.RawUrl - Return complete URL of Current page

RouteData.Values - Return Collection of Values of URL

Request.Params - Return Name Value Collections

Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
yogesh lodha
  • 191
  • 1
  • 4
1

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))
{

}
1

You can get these parameter list in ControllerContext.RoutValues object as key-value pair.

You can store it in some variable and you make use of that variable in your logic.

Zorayr
  • 23,770
  • 8
  • 136
  • 129
0

In order 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) {}
CarenRose
  • 1,266
  • 1
  • 12
  • 24
Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
-5

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.

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

Stofke
  • 2,928
  • 2
  • 21
  • 28
  • 5
    This is not necessary in .NET and MVC, the `RouteData` and `Request.Url` objects contain the data required already split into parts for you. – David Glenn Feb 15 '11 at 14:19