使电流控制器和操作发送到路由 [英] Getting the Current controller and action sent to the Route
问题描述
我试图获取在路由中调用的当前控制器和操作。在我的Global.asax.cs我有以下使用和以下行:
I am attempting to Get the current controller and action being called in the route. In my Global.asax.cs I have the following using and the following line:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IProwlAdminUI.Models;
*** LINE WITH ISSUE***
string action = ViewContext.RouteData.Values["controller"].ToString() + ViewContext.RouteData.Values["action"].ToString()
*******
现在ViewContext.RouteData给我一个对象引用是非静态字段,方法或属性System.Web.Mvc.ControllerContext.RouteData.Get'
Right now the ViewContext.RouteData is giving me "an object reference is required for the non-static field, method, or property 'System.Web.Mvc.ControllerContext.RouteData.Get'"
我需要以获得此值,以便我可以使用控制器和操作来评估用户的权限,并查看他们是否可以执行他们尝试执行的操作。
I need to get this so that I can use the controller and action to evaluate permissions for the user and see if they can perform the action they are attempting to do.
任何帮助非常感谢。
感谢
推荐答案
你想要做什么,因为Global.asax中的代码只有在应用程序启动时才执行一次。这发生在传入请求的上下文可用之前,这就是为什么您不能访问RouteData值。
You cannot do what you are trying to do because the code in Global.asax executes only once when your application starts up. This happens before the context of an incoming request is available and that is why you cannot access the RouteData values.
在更一般的注意事项上,您可以编写自定义AuthorizationAttribute做你想要的。请参阅以下链接以获取示例: http:// davidhayden。 com / blog / dave / archive / 2009/04/09 / CustomAuthorizationASPNETMVCFrameworkAuthorizeAttribute.aspx
On a more general note, you can write a custom AuthorizationAttribute to do what you want. See the following link for an example: http://davidhayden.com/blog/dave/archive/2009/04/09/CustomAuthorizationASPNETMVCFrameworkAuthorizeAttribute.aspx
这篇关于使电流控制器和操作发送到路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!