ASP.NET 剃刀获取 URL 中的区域
本文关键字:区域 URL 获取 NET 剃刀 ASP | 更新日期: 2023-09-27 17:56:57
有没有办法使用 ASP.NET Razor 语法从请求中获取区域组件?
http://localhost:12345/SomeController/SomeAction
http://localhost:12345/MyArea/AnotherController/AnotherAction
我想使用 Razor 干净可靠地获取 URL 的"MyArea"部分。
最好的方法是什么?
我实际上使用了这个:
var area = ViewContext.RouteData.DataTokens["area"];
我在另一篇文章中找到了这一点:
指向 RouteValueDictionary 的字符串 URL
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));
然后要获取区域:
string area = routeData.DataTokens["area"].ToString();
或者,您可以只使用:
var action = RouteData.Values["area"];
祝你好运。
这应该为您完成。
@ViewContext.Controller.ValueProvider.GetValue("area").RawValue.ToString()