Ozzie Perez
Ozzie Perez

Reputation: 583

How to parse a string URL into MVC Route Values (area, controller, action, querystring)?

Is there a method to extract the area, controller, action, and querystring from a URL in ASP.NET MVC? Don't want to reinvent the wheel implementing my own if there's already a way to do it.

Thanks!

Upvotes: 8

Views: 15204

Answers (2)

Ozzie Perez
Ozzie Perez

Reputation: 583

I was able to get it from here:

String URL to RouteValueDictionary

To get the area from this example I used:

string area = routeData.DataTokens["area"].ToString();

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1038820

You could pull this information from the routes:

var controller = RouteData.Values["controller"];
var action = RouteData.Values["action"];
var action = RouteData.Values["area"];

As far as the query string is concerned you could pull it from the Request:

var queryString = Request.Url.Query;

UPDATE:

If the url is coming from a DB:

var uri = new Uri(someStringThatRepresentsTheUrlAndComesFromADb);
var queryString = uri.Query;

Upvotes: -2

Related Questions