我在控制器中定义了一条路线,
[Route("AboutUs/Profile")]
[Route("AboutUs/Organization")]
public ActionResult Contents()
{
string _controller = RouteData.Route.GetRouteData(this.HttpContext).Values["controller"].ToString();
string _action = RouteData.Route.GetRouteData(this.HttpContext).Values["action"].ToString();
}
但是_action == "Contents"
,我怎样才能获得_action = "Profile"
?
我试过了:
string _controller = this.ControllerContext.RouteData.Values["controller"].ToString();
string _action = this.ControllerContext.RouteData.Values["action"].ToString();
和这个
string _controller = this.RouteData.Values["controller"].ToString();
string _action = this.RouteData.Values["_action"].ToString();
但所有这些都不起作用。
答案 0 :(得分:0)
我编写了用于记录操作的代码:
public class PreviousPage
{
public string Action { get; set; }
public string Controller { get; set; }
public object Parameters { get; set; }
public PreviousPage() { PP(); }
public PreviousPage(string action, string controller, object parameters)
{
Action = action;
Controller = controller;
Parameters = parameters;
}
public void PP()
{
var htpc = HttpContext.Current.Request.RequestContext.RouteData.Values;
object controller;
htpc.TryGetValue("controller", out controller);
Controller = controller as string;
object action;
htpc.TryGetValue("action", out action);
Action = action as string;
object id;
htpc.TryGetValue("id", out id);
int q = HttpContext.Current.Request.RawUrl.IndexOf('?');
if (q>-1)
Parameters = HttpContext.Current.Request.RawUrl.Substring(q+1);
else
Parameters = id as string;
}
}
这是为了改变,但经过一些修改,你就会到达那里。
编辑:
沿着静态函数获取动作:
public static string GetAction()
{
object action=null;
HttpContext.Current.Request.RequestContext.RouteData.Values.TryGetValue("action", out action);
return action as string;
}