2

Is there a way using the ASP.NET Razor Syntax to get the area component from a request?

http://localhost:12345/SomeController/SomeAction
http://localhost:12345/MyArea/AnotherController/AnotherAction

I'd like to use Razor to cleanly and reliably get me the "MyArea" part of the URL.

What's the best approach for this?

Adam Levitt
  • 10,316
  • 26
  • 84
  • 145

3 Answers3

3

I actually used this:

var area = ViewContext.RouteData.DataTokens["area"];
Adam Levitt
  • 10,316
  • 26
  • 84
  • 145
  • Thanks for this! Most of what I have read has said create a html helper class. Soooooo much code for a simple thing. Thanks for keeping it simple! Exactly what I needed. – HKstrongside Feb 28 '16 at 18:52
2

I found this on another post:

String URL to RouteValueDictionary

var request = new HttpRequest(null, "http://localhost:3333/Home/About", "testvalue=1");
var response = new HttpResponse(new StringWriter());
var httpContext = new HttpContext(request, response);
var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));

And then to get the area:

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

Alternatively, you might be able to just use:

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

Good luck.

Community
  • 1
  • 1
sgeddes
  • 62,311
  • 6
  • 61
  • 83
0

This should do it for you.

@ViewContext.Controller.ValueProvider.GetValue("area").RawValue.ToString()
Becuzz
  • 6,846
  • 26
  • 39